# Add the original request to any HTTPError unless
# there'a already a request attached in the case of
# a ProxyError.
- if exc.request is None:
- exc.request = request
+ if exc._request is None:
+ exc._request = request
raise
self.cookies.extract_cookies(response)
# Add the original request to any HTTPError unless
# there'a already a request attached in the case of
# a ProxyError.
- if exc.request is None:
- exc.request = request
+ if exc._request is None:
+ exc._request = request
raise
self.cookies.extract_cookies(response)
def __init__(
self, *args: typing.Any, request: "Request" = None, response: "Response" = None
) -> None:
- self.response = response
- self.request = request or getattr(self.response, "request", None)
super().__init__(*args)
+ self._request = request or (response.request if response is not None else None)
+ self.response = response
+
+ @property
+ def request(self) -> "Request":
+ # NOTE: this property exists so that a `Request` is exposed to type
+ # checkers, instead of `Optional[Request]`.
+ assert self._request is not None # Populated by the client.
+ return self._request
# Timeout exceptions...