jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1318204?usp=email )
Change subject: citoid: raise CitoidError on error response ......................................................................
citoid: raise CitoidError on error response
Raise new implemented CitoidError when the Citoid service returns an error response. This prevents confusing error messages caused by unexpected response.
Bug: T433230 Change-Id: Ib9c2fd98343946f9dbc23597426e01ccfef95371 --- M pywikibot/data/citoid.py M pywikibot/exceptions.py 2 files changed, 27 insertions(+), 9 deletions(-)
Approvals: jenkins-bot: Verified Strainu: Looks good to me, approved
diff --git a/pywikibot/data/citoid.py b/pywikibot/data/citoid.py index 6f0ceb7..99afefc 100644 --- a/pywikibot/data/citoid.py +++ b/pywikibot/data/citoid.py @@ -13,9 +13,8 @@ from dataclasses import dataclass from typing import Any
-import pywikibot from pywikibot.comms import http -from pywikibot.exceptions import ApiNotAvailableError, Error +from pywikibot.exceptions import ApiNotAvailableError, CitoidError from pywikibot.site import BaseSite
@@ -41,9 +40,19 @@ ) -> dict[str, Any]: """Get a citation from the citoid service.
- :param response_format: Return format, e.g. 'bibtex', 'wikibase', etc. + .. version-changed:: 11.7 + Raise :exc:`CitoidError` if the Citoid service returns an + error with the response dict. + + :param response_format: Return format, e.g. 'bibtex', 'wikibase', + etc. :param ref_url: The URL to get the citation for. :return: A dictionary with the citation data. + :raises ApiNotAvailableError: Citoid endpoint not configured for + the given site. + :raises CitoidError: Raised with the error returned by the + Citoid service. + :raises ValueError: Invalid format for *response_format*. """ if response_format not in VALID_FORMAT: raise ValueError(f'Invalid format {response_format}, ' @@ -56,9 +65,9 @@ ref_url = urllib.parse.quote(ref_url, safe='') api_url = urllib.parse.urljoin(base_url, f'{response_format}/{ref_url}') - try: - json = http.request(self.site, api_url).json() - return json - except Error as e: - pywikibot.log(f'Caught pywikibot error {e}') - raise + data = http.request(self.site, api_url).json() + + if 'error' in data: + raise CitoidError(data['error']) + + return data diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py index 9cd75cb..30af4ad 100644 --- a/pywikibot/exceptions.py +++ b/pywikibot/exceptions.py @@ -15,6 +15,7 @@ | └── UploadError ├── AutoblockUserError ├── CaptchaError + ├── CitoidError ├── ClientError | └── Client414Error ├── InvalidTitleError @@ -289,6 +290,14 @@ return self.info
+class CitoidError(Error): + + """The Citoid service returned an error. + + .. version-added:: 11.7 + """ + + class PageRelatedError(Error):
"""Abstract Exception, used when the exception concerns a particular Page.
pywikibot-commits@lists.wikimedia.org