From: Tom Christie Date: Mon, 3 Aug 2020 19:06:18 +0000 (+0100) Subject: Keep HTTPError as a base class for .request() and .raise_for_status() (#1125) X-Git-Tag: 0.14.0~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7aa6e018913349166fba64cd5f3a9daba7364fb;p=thirdparty%2Fhttpx.git Keep HTTPError as a base class for .request() and .raise_for_status() (#1125) --- diff --git a/httpx/_exceptions.py b/httpx/_exceptions.py index 36fa7d11..4c22dd73 100644 --- a/httpx/_exceptions.py +++ b/httpx/_exceptions.py @@ -1,32 +1,33 @@ """ Our exception hierarchy: -* RequestError - + TransportError - - TimeoutException - · ConnectTimeout - · ReadTimeout - · WriteTimeout - · PoolTimeout - - NetworkError - · ConnectError - · ReadError - · WriteError - · CloseError - - ProxyError - - ProtocolError - + DecodingError - + TooManyRedirects - + RequestBodyUnavailable - + InvalidURL -* HTTPStatusError +* HTTPError + x RequestError + + TransportError + - TimeoutException + · ConnectTimeout + · ReadTimeout + · WriteTimeout + · PoolTimeout + - NetworkError + · ConnectError + · ReadError + · WriteError + · CloseError + - ProtocolError + - ProxyError + + DecodingError + + TooManyRedirects + + RequestBodyUnavailable + + InvalidURL + x HTTPStatusError * NotRedirectResponse * CookieConflict * StreamError - + StreamConsumed - + ResponseNotRead - + RequestNotRead - + ResponseClosed + x StreamConsumed + x ResponseNotRead + x RequestNotRead + x ResponseClosed """ import contextlib import typing @@ -37,9 +38,20 @@ if typing.TYPE_CHECKING: from ._models import Request, Response # pragma: nocover -class RequestError(Exception): +class HTTPError(Exception): """ - Base class for all exceptions that may occur when issuing a `.request()`. + Base class for `RequestError` and `HTTPStatusError`. + + Useful for `try...except` blocks when issuing a request, + and then calling .raise_for_status(). + + For example: + + try: + response = httpx.get("https://www.example.com") + response.raise_for_status() + except httpx.HTTPError as exc: + print(f"HTTP Exception for {exc.request.url} - {exc.message}") """ def __init__(self, message: str, *, request: "Request") -> None: @@ -47,6 +59,15 @@ class RequestError(Exception): self.request = request +class RequestError(HTTPError): + """ + Base class for all exceptions that may occur when issuing a `.request()`. + """ + + def __init__(self, message: str, *, request: "Request") -> None: + super().__init__(message, request=request) + + class TransportError(RequestError): """ Base class for all exceptions that are mapped from the httpcore API. @@ -169,7 +190,7 @@ class InvalidURL(RequestError): # Client errors -class HTTPStatusError(Exception): +class HTTPStatusError(HTTPError): """ Response sent an error HTTP status. @@ -179,8 +200,7 @@ class HTTPStatusError(Exception): def __init__( self, message: str, *, request: "Request", response: "Response" ) -> None: - super().__init__(message) - self.request = request + super().__init__(message, request=request) self.response = response @@ -277,11 +297,6 @@ class ResponseClosed(StreamError): super().__init__(message) -# We're continuing to expose this earlier naming at the moment. -# It is due to be deprecated. Don't use it. -HTTPError = RequestError - - @contextlib.contextmanager def map_exceptions( mapping: typing.Mapping[typing.Type[Exception], typing.Type[Exception]],