From: Tom Christie Date: Thu, 23 Jul 2020 08:55:07 +0000 (+0100) Subject: Refactor map_exceptions (#1076) X-Git-Tag: 0.14.0~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=01287bac9999e3bc34171744e2a87064e0bbc2a1;p=thirdparty%2Fhttpx.git Refactor map_exceptions (#1076) --- diff --git a/httpx/_client.py b/httpx/_client.py index 66b4805d..0eac03b7 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -20,7 +20,6 @@ from ._config import ( from ._content_streams import ContentStream from ._exceptions import ( HTTPCORE_EXC_MAP, - HTTPError, InvalidURL, RequestBodyUnavailable, TooManyRedirects, @@ -691,28 +690,20 @@ class Client(BaseClient): transport = self._transport_for_url(request.url) - try: - with map_exceptions(HTTPCORE_EXC_MAP): - ( - http_version, - status_code, - reason_phrase, - headers, - stream, - ) = transport.request( - request.method.encode(), - request.url.raw, - headers=request.headers.raw, - stream=request.stream, - timeout=timeout.as_dict(), - ) - except HTTPError as exc: - # 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 - raise + with map_exceptions(HTTPCORE_EXC_MAP, request=request): + ( + http_version, + status_code, + reason_phrase, + headers, + stream, + ) = transport.request( + request.method.encode(), + request.url.raw, + headers=request.headers.raw, + stream=request.stream, + timeout=timeout.as_dict(), + ) response = Response( status_code, http_version=http_version.decode("ascii"), @@ -1232,28 +1223,20 @@ class AsyncClient(BaseClient): transport = self._transport_for_url(request.url) - try: - with map_exceptions(HTTPCORE_EXC_MAP): - ( - http_version, - status_code, - reason_phrase, - headers, - stream, - ) = await transport.request( - request.method.encode(), - request.url.raw, - headers=request.headers.raw, - stream=request.stream, - timeout=timeout.as_dict(), - ) - except HTTPError as exc: - # 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 - raise + with map_exceptions(HTTPCORE_EXC_MAP, request=request): + ( + http_version, + status_code, + reason_phrase, + headers, + stream, + ) = await transport.request( + request.method.encode(), + request.url.raw, + headers=request.headers.raw, + stream=request.stream, + timeout=timeout.as_dict(), + ) response = Response( status_code, http_version=http_version.decode("ascii"), diff --git a/httpx/_exceptions.py b/httpx/_exceptions.py index f36fafb6..a5271c37 100644 --- a/httpx/_exceptions.py +++ b/httpx/_exceptions.py @@ -216,7 +216,8 @@ class CookieConflict(HTTPError): @contextlib.contextmanager def map_exceptions( - mapping: typing.Mapping[typing.Type[Exception], typing.Type[Exception]] + mapping: typing.Mapping[typing.Type[Exception], typing.Type[Exception]], + **kwargs: typing.Any, ) -> typing.Iterator[None]: try: yield @@ -235,7 +236,8 @@ def map_exceptions( if mapped_exc is None: raise - raise mapped_exc(exc) from None + message = str(exc) + raise mapped_exc(message, **kwargs) from None # type: ignore HTTPCORE_EXC_MAP = {