Keep HTTPError as a base class for .request() and .raise_for_status() (#1125)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 0fb28e8 · Browse files · 1 file·+48 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+48 added−33 removed
- other:+48 added−33 removed
Provenance
- Commit
d7aa6e0- Tree
b75386f- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
0fb28e8
1 file changed+48 added−33 removed
modified:httpx/_exceptions.py+48 added-33 removed
1 """2 Our exception hierarchy:3 4+* HTTPError4-* RequestError5- + TransportError6- - TimeoutException7- · ConnectTimeout8- · ReadTimeout9- · WriteTimeout10- · PoolTimeout11- - NetworkError12- · ConnectError13- · ReadError14- · WriteError15- · CloseError16- - ProxyError17- - ProtocolError18- + DecodingError19- + TooManyRedirects20- + RequestBodyUnavailable21- + InvalidURL22-* HTTPStatusError5+ x RequestError6+ + TransportError7+ - TimeoutException8+ · ConnectTimeout9+ · ReadTimeout10+ · WriteTimeout11+ · PoolTimeout12+ - NetworkError13+ · ConnectError14+ · ReadError15+ · WriteError16+ · CloseError17+ - ProtocolError18+ - ProxyError19+ + DecodingError20+ + TooManyRedirects21+ + RequestBodyUnavailable22+ + InvalidURL23+ x HTTPStatusError24 * NotRedirectResponse25 * CookieConflict26 * StreamError26- + StreamConsumed27- + ResponseNotRead28- + RequestNotRead29- + ResponseClosed27+ x StreamConsumed28+ x ResponseNotRead29+ x RequestNotRead30+ x ResponseClosed31 """32 import contextlib33 import typing34 2 unmodified lines37 if typing.TYPE_CHECKING:38 from ._models import Request, Response # pragma: nocover39 40 40-class RequestError(Exception):41+class HTTPError(Exception):42 """42- Base class for all exceptions that may occur when issuing a `.request()`.43+ Base class for `RequestError` and `HTTPStatusError`.44+45+ Useful for `try...except` blocks when issuing a request,46+ and then calling .raise_for_status().47+48+ For example:49+50+ try:51+ response = httpx.get("https://www.example.com")52+ response.raise_for_status()53+ except httpx.HTTPError as exc:54+ print(f"HTTP Exception for {exc.request.url} - {exc.message}")55 """56 57 def __init__(self, message: str, *, request: "Request") -> None:58 super().__init__(message)59 self.request = request60 61 62+class RequestError(HTTPError):63+ """64+ Base class for all exceptions that may occur when issuing a `.request()`.65+ """66+67+ def __init__(self, message: str, *, request: "Request") -> None:68+ super().__init__(message, request=request)69+70+71 class TransportError(RequestError):72 """73 Base class for all exceptions that are mapped from the httpcore API.74 """114 unmodified lines189 190 # Client errors191 192 172-class HTTPStatusError(Exception):193+class HTTPStatusError(HTTPError):194 """195 Response sent an error HTTP status.196 197 May be raised when calling `response.raise_for_status()`1 unmodified line199 200 def __init__(201 self, message: str, *, request: "Request", response: "Response"202 ) -> None:182- super().__init__(message)183- self.request = request203+ super().__init__(message, request=request)204 self.response = response205 206 207 class NotRedirectResponse(Exception):88 unmodified lines296 )297 super().__init__(message)298 299 280-# We're continuing to expose this earlier naming at the moment.281-# It is due to be deprecated. Don't use it.282-HTTPError = RequestError283-284-300 @contextlib.contextmanager301 def map_exceptions(302 mapping: typing.Mapping[typing.Type[Exception], typing.Type[Exception]],303 **kwargs: typing.Any,36 unmodified lines