Refactor map_exceptions (#1076)
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 247ee0d · Browse files · 2 files·+32 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−47 removed
- other:+32 added−47 removed
Provenance
- Commit
01287ba- Tree
6e7df3c- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
247ee0d
2 files changed+32 added−47 removed
modified:httpx/_client.py+28 added-45 removed
18 unmodified lines19 )20 from ._content_streams import ContentStream21 from ._exceptions import (22 HTTPCORE_EXC_MAP,23- HTTPError,23 InvalidURL,24 RequestBodyUnavailable,25 TooManyRedirects,26 map_exceptions,662 unmodified lines689 """690 691 transport = self._transport_for_url(request.url)692 694- try:695- with map_exceptions(HTTPCORE_EXC_MAP):696- (697- http_version,698- status_code,699- reason_phrase,700- headers,701- stream,702- ) = transport.request(703- request.method.encode(),704- request.url.raw,705- headers=request.headers.raw,706- stream=request.stream,707- timeout=timeout.as_dict(),708- )693+ with map_exceptions(HTTPCORE_EXC_MAP, request=request):694+ (695+ http_version,696+ status_code,697+ reason_phrase,698+ headers,699+ stream,700+ ) = transport.request(701+ request.method.encode(),702+ request.url.raw,703+ headers=request.headers.raw,704+ stream=request.stream,705+ timeout=timeout.as_dict(),706+ )709- except HTTPError as exc:710- # Add the original request to any HTTPError unless711- # there'a already a request attached in the case of712- # a ProxyError.713- if exc._request is None:714- exc._request = request715- raise707 response = Response(708 status_code,709 http_version=http_version.decode("ascii"),710 headers=headers,511 unmodified lines1222 """1223 1224 transport = self._transport_for_url(request.url)1225 1235- try:1236- with map_exceptions(HTTPCORE_EXC_MAP):1237- (1238- http_version,1239- status_code,1240- reason_phrase,1241- headers,1242- stream,1243- ) = await transport.request(1244- request.method.encode(),1245- request.url.raw,1246- headers=request.headers.raw,1247- stream=request.stream,1248- timeout=timeout.as_dict(),1249- )1226+ with map_exceptions(HTTPCORE_EXC_MAP, request=request):1227+ (1228+ http_version,1229+ status_code,1230+ reason_phrase,1231+ headers,1232+ stream,1233+ ) = await transport.request(1234+ request.method.encode(),1235+ request.url.raw,1236+ headers=request.headers.raw,1237+ stream=request.stream,1238+ timeout=timeout.as_dict(),1239+ )1250- except HTTPError as exc:1251- # Add the original request to any HTTPError unless1252- # there'a already a request attached in the case of1253- # a ProxyError.1254- if exc._request is None:1255- exc._request = request1256- raise1240 response = Response(1241 status_code,1242 http_version=http_version.decode("ascii"),1243 headers=headers,260 unmodified linesmodified:httpx/_exceptions.py+4 added-2 removed
214 unmodified lines215 216 217 @contextlib.contextmanager218 def map_exceptions(219- mapping: typing.Mapping[typing.Type[Exception], typing.Type[Exception]]219+ mapping: typing.Mapping[typing.Type[Exception], typing.Type[Exception]],220+ **kwargs: typing.Any,221 ) -> typing.Iterator[None]:222 try:223 yield224 except Exception as exc:10 unmodified lines235 236 if mapped_exc is None:237 raise238 238- raise mapped_exc(exc) from None239+ message = str(exc)240+ raise mapped_exc(message, **kwargs) from None # type: ignore241 242 243 HTTPCORE_EXC_MAP = {244 httpcore.TimeoutException: TimeoutException,12 unmodified lines