Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep HTTPError as a base class for .request() and .raise_for_status() #1125

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions httpx/_exceptions.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -37,16 +38,36 @@
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:
super().__init__(message)
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.
Expand Down Expand Up @@ -169,7 +190,7 @@ class InvalidURL(RequestError):
# Client errors


class HTTPStatusError(Exception):
class HTTPStatusError(HTTPError):
"""
Response sent an error HTTP status.

Expand All @@ -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


Expand Down Expand Up @@ -277,11 +297,6 @@ def __init__(self) -> None:
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]],
Expand Down