jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/894207 )
Change subject: [IMPR] Add ClientError for http status 4XX ......................................................................
[IMPR] Add ClientError for http status 4XX
- add ClientError for http status 400-451 - rename Server414Error to Client414Error - raise ClientError with site_detect.check_response() in addition to ServerError - raise Client414Error with http.error_handling_callback() - check for Client414Error with Request._http_request()
Change-Id: Ie125730837db50d4143d9b4f656b2bd4d9f01430 --- M pywikibot/exceptions.py M pywikibot/comms/http.py M pywikibot/site_detect.py M pywikibot/data/api/_requests.py 4 files changed, 81 insertions(+), 37 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py index 77128be..319f512 100644 --- a/pywikibot/comms/http.py +++ b/pywikibot/comms/http.py @@ -48,8 +48,8 @@ from pywikibot import config, tools from pywikibot.backports import Tuple from pywikibot.exceptions import ( + Client414Error, FatalServerError, - Server414Error, Server504Error, ServerError, ) @@ -333,7 +333,7 @@ raise response from None
if response.status_code == HTTPStatus.REQUEST_URI_TOO_LONG: - raise Server414Error('Too long GET request') + raise Client414Error(HTTPStatus(response.status_code).description)
if response.status_code == HTTPStatus.GATEWAY_TIMEOUT: raise Server504Error('Server {} timed out' diff --git a/pywikibot/data/api/_requests.py b/pywikibot/data/api/_requests.py index 9d4dc8e..ebdbe9e 100644 --- a/pywikibot/data/api/_requests.py +++ b/pywikibot/data/api/_requests.py @@ -25,11 +25,11 @@ from pywikibot.backports import Callable, Dict, Match, Tuple, removeprefix from pywikibot.comms import http from pywikibot.exceptions import ( + Client414Error, Error, FatalServerError, MaxlagTimeoutError, NoUsernameError, - Server414Error, Server504Error, SiteDefinitionError, TimeoutError, @@ -687,13 +687,13 @@ data=data, headers=headers) except Server504Error: pywikibot.log('Caught HTTP 504 error; retrying') - except Server414Error: + except Client414Error: if use_get: pywikibot.log('Caught HTTP 414 error; retrying') use_get = False else: - pywikibot.warning('Caught HTTP 414 error, although not ' - 'using GET.') + pywikibot.warning( + 'Caught HTTP 414 error, although not using GET.') raise except (ConnectionError, FatalServerError): # This error is not going to be fixed by just waiting diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py index 654e579..36da84e 100644 --- a/pywikibot/exceptions.py +++ b/pywikibot/exceptions.py @@ -10,6 +10,8 @@ | +-- UploadError +-- AutoblockUserError +-- CaptchaError + +-- ClientError + | +-- Client414Error +-- InvalidTitleError +-- NoUsernameError +-- PageInUseError @@ -41,7 +43,6 @@ +-- SectionError +-- ServerError | +-- FatalServerError - | +-- Server414Error | +-- Server504Error +-- SiteDefinitionError | +-- UnknownFamilyError @@ -68,17 +69,18 @@
Error: Base class, all exceptions should the subclass of this class.
- - NoUsernameError: Username is not in user config file, or it is invalid. - - AutoblockUserError: requested action on a virtual autoblock user not valid - - TranslationError: no language translation found - - UserRightsError: insufficient rights for requested action - - InvalidTitleError: Invalid page title - CaptchaError: Captcha is asked and config.solve_captcha == False - - i18n.TranslationError: i18n/l10n message not available + - ClientError: A problem with the client request + - AutoblockUserError: requested action on a virtual autoblock user not valid + - InvalidTitleError: Invalid page title + - NoUsernameError: Username is not in user config file, or it is invalid. - PageInUseError: Page cannot be reserved due to a lock - - UnknownExtensionError: Extension is not defined for this site - - VersionParseError: failed to parse version information - SectionError: The section specified by # does not exist + - TranslationError: no language translation found + - UnknownExtensionError: Extension is not defined for this site + - UserRightsError: insufficient rights for requested action + - VersionParseError: failed to parse version information + - i18n.TranslationError: i18n/l10n message not available
APIError: wiki API returned an error
@@ -160,9 +162,13 @@ All Pywikibot Error exceptions must be imported from ``pywikibot.exceptions``. Deprecated exceptions identifiers were removed. + +.. versionchanged:: 8.1 + ``Server414Error`` class is deprecated; use :class:`Client414Error` + instead. """ # -# (C) Pywikibot team, 2008-2022 +# (C) Pywikibot team, 2008-2023 # # Distributed under the terms of the MIT license. # @@ -170,7 +176,7 @@ from typing import Any, Optional, Union
import pywikibot -from pywikibot.tools import issue_deprecation_warning +from pywikibot.tools import ModuleDeprecationWrapper, issue_deprecation_warning from pywikibot.tools._deprecate import _NotImplementedWarning
@@ -576,6 +582,22 @@ message = 'Page {} is title-blacklisted.'
+class ClientError(Error): + + """Got unexpected server response due to client issue. + + .. versionadded:: 8.1 + """ + + +class Client414Error(ClientError): + + """Server returned with HTTP 414 code. + + .. versionadded:: 8.1 + """ + + class ServerError(Error):
"""Got unexpected server response.""" @@ -591,11 +613,6 @@ """Server timed out with HTTP 504 code."""
-class Server414Error(ServerError): - - """Server returned with HTTP 414 code.""" - - class CaptchaError(Error):
"""Captcha is asked and config.solve_captcha == False.""" @@ -681,3 +698,8 @@ class MaxlagTimeoutError(TimeoutError):
"""Request failed with a maxlag timeout error.""" + + +wrapper = ModuleDeprecationWrapper(__name__) +wrapper.add_deprecated_attr( + 'Server414Error', Client414Error, since='8.1.0') diff --git a/pywikibot/site_detect.py b/pywikibot/site_detect.py index 8a266d2..5dde965 100644 --- a/pywikibot/site_detect.py +++ b/pywikibot/site_detect.py @@ -17,7 +17,7 @@ import pywikibot from pywikibot.backports import removesuffix from pywikibot.comms.http import fetch -from pywikibot.exceptions import ServerError +from pywikibot.exceptions import ClientError, ServerError from pywikibot.tools import MediaWikiVersion
@@ -280,25 +280,31 @@
def check_response(response): - """Raise ServerError if the response indicates a server error. + """Raise ClientError or ServerError depending on http status.
.. versionadded:: 3.0 .. versionchanged:: 7.0 - Raise a generic ServerError if http status code is not - IANA-registered but unofficial code - - + Raise a generic :class:`exceptions.ServerError` if http status + code is not IANA-registered but unofficial code + .. versionchanged:: 8.1 + Raise a :class:`exceptions.ClientError` if status code is 4XX """ - if response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR: - try: - msg = HTTPStatus(response.status_code).phrase - except ValueError as err: - m = re.search(r'\d{3}', err.args[0], flags=re.ASCII) - if not m: - raise err - msg = f'Generic Server Error ({m.group()})' + for status_code, err_class, err_type in [ + (HTTPStatus.INTERNAL_SERVER_ERROR, ServerError, 'Server'), + (HTTPStatus.BAD_REQUEST, ClientError, 'Client') + ]: # highest http status code first + if response.status_code >= status_code: + try: + status = HTTPStatus(response.status_code).description + except ValueError as err: + m = re.search(r'\d{3}', err.args[0], flags=re.ASCII) + if not m: + raise err + msg = f'Generic {err_type} Error ({m.group()})' + else: + msg = f'({status}) {status.description}'
- raise ServerError(msg) + raise err_class(msg)
if response.status_code == HTTPStatus.OK \ and SERVER_DB_ERROR_MSG in response.text:
pywikibot-commits@lists.wikimedia.org