This commit is contained in:
Iliyan Angelov
2025-12-01 06:50:10 +02:00
parent 91f51bc6fe
commit 62c1fe5951
4682 changed files with 544807 additions and 31208 deletions

View File

@@ -30,12 +30,46 @@ Our exception hierarchy:
x ResponseNotRead
x RequestNotRead
"""
from __future__ import annotations
import contextlib
import typing
if typing.TYPE_CHECKING:
from ._models import Request, Response # pragma: no cover
__all__ = [
"CloseError",
"ConnectError",
"ConnectTimeout",
"CookieConflict",
"DecodingError",
"HTTPError",
"HTTPStatusError",
"InvalidURL",
"LocalProtocolError",
"NetworkError",
"PoolTimeout",
"ProtocolError",
"ProxyError",
"ReadError",
"ReadTimeout",
"RemoteProtocolError",
"RequestError",
"RequestNotRead",
"ResponseNotRead",
"StreamClosed",
"StreamConsumed",
"StreamError",
"TimeoutException",
"TooManyRedirects",
"TransportError",
"UnsupportedProtocol",
"WriteError",
"WriteTimeout",
]
class HTTPError(Exception):
"""
@@ -57,16 +91,16 @@ class HTTPError(Exception):
def __init__(self, message: str) -> None:
super().__init__(message)
self._request: typing.Optional["Request"] = None
self._request: Request | None = None
@property
def request(self) -> "Request":
def request(self) -> Request:
if self._request is None:
raise RuntimeError("The .request property has not been set.")
return self._request
@request.setter
def request(self, request: "Request") -> None:
def request(self, request: Request) -> None:
self._request = request
@@ -75,9 +109,7 @@ class RequestError(HTTPError):
Base class for all exceptions that may occur when issuing a `.request()`.
"""
def __init__(
self, message: str, *, request: typing.Optional["Request"] = None
) -> None:
def __init__(self, message: str, *, request: Request | None = None) -> None:
super().__init__(message)
# At the point an exception is raised we won't typically have a request
# instance to associate it with.
@@ -230,9 +262,7 @@ class HTTPStatusError(HTTPError):
May be raised when calling `response.raise_for_status()`
"""
def __init__(
self, message: str, *, request: "Request", response: "Response"
) -> None:
def __init__(self, message: str, *, request: Request, response: Response) -> None:
super().__init__(message)
self.request = request
self.response = response
@@ -313,7 +343,10 @@ class ResponseNotRead(StreamError):
"""
def __init__(self) -> None:
message = "Attempted to access streaming response content, without having called `read()`."
message = (
"Attempted to access streaming response content,"
" without having called `read()`."
)
super().__init__(message)
@@ -323,13 +356,16 @@ class RequestNotRead(StreamError):
"""
def __init__(self) -> None:
message = "Attempted to access streaming request content, without having called `read()`."
message = (
"Attempted to access streaming request content,"
" without having called `read()`."
)
super().__init__(message)
@contextlib.contextmanager
def request_context(
request: typing.Optional["Request"] = None,
request: Request | None = None,
) -> typing.Iterator[None]:
"""
A context manager that can be used to attach the given request context