Skip to content

Commit 7bcdd92

Browse files
committed
fix: error while handling http exceptions
1 parent 9cd2cc0 commit 7bcdd92

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
'fastcore.net': { 'fastcore.net.HTTP4xxClientError': ('net.html#http4xxclienterror', 'fastcore/net.py'),
334334
'fastcore.net.HTTP5xxServerError': ('net.html#http5xxservererror', 'fastcore/net.py'),
335335
'fastcore.net.Request.summary': ('net.html#request.summary', 'fastcore/net.py'),
336+
'fastcore.net._mk_error': ('net.html#_mk_error', 'fastcore/net.py'),
336337
'fastcore.net._socket_det': ('net.html#_socket_det', 'fastcore/net.py'),
337338
'fastcore.net.do_request': ('net.html#do_request', 'fastcore/net.py'),
338339
'fastcore.net.start_client': ('net.html#start_client', 'fastcore/net.py'),

fastcore/net.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ def urlopener():
8989
(431,'Header Fields Too Large'),(451,'Legal Reasons')
9090
)
9191

92+
def _mk_error(nm, code, msg):
93+
cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)
94+
def _init(self, url, hdrs, fp, msg=msg, code=code): super(cls, self).__init__(url, code, msg, hdrs, fp)
95+
cls.__init__ = _init
96+
return cls
97+
9298
for code,msg in _httperrors:
9399
nm = f'HTTP{code}{msg.replace(" ","")}Error'
94-
cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)
95-
globals()[nm] = ExceptionsHTTP[code] = cls
100+
globals()[nm] = ExceptionsHTTP[code] = _mk_error(nm, code, msg)
96101

97102
# %% ../nbs/03b_net.ipynb 16
98103
_all_ = ['HTTP400BadRequestError', 'HTTP401UnauthorizedError', 'HTTP402PaymentRequiredError', 'HTTP403ForbiddenError', 'HTTP404NotFoundError', 'HTTP405MethodNotAllowedError', 'HTTP406NotAcceptableError', 'HTTP407ProxyAuthRequiredError', 'HTTP408RequestTimeoutError', 'HTTP409ConflictError', 'HTTP410GoneError', 'HTTP411LengthRequiredError', 'HTTP412PreconditionFailedError', 'HTTP413PayloadTooLargeError', 'HTTP414URITooLongError', 'HTTP415UnsupportedMediaTypeError', 'HTTP416RangeNotSatisfiableError', 'HTTP417ExpectationFailedError', 'HTTP418AmAteapotError', 'HTTP421MisdirectedRequestError', 'HTTP422UnprocessableEntityError', 'HTTP423LockedError', 'HTTP424FailedDependencyError', 'HTTP425TooEarlyError', 'HTTP426UpgradeRequiredError', 'HTTP428PreconditionRequiredError', 'HTTP429TooManyRequestsError', 'HTTP431HeaderFieldsTooLargeError', 'HTTP451LegalReasonsError']

nbs/03b_net.ipynb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,15 @@
284284
" (431,'Header Fields Too Large'),(451,'Legal Reasons')\n",
285285
")\n",
286286
"\n",
287+
"def _mk_error(nm, code, msg):\n",
288+
" cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)\n",
289+
" def _init(self, url, hdrs, fp, msg=msg, code=code): super(cls, self).__init__(url, code, msg, hdrs, fp)\n",
290+
" cls.__init__ = _init\n",
291+
" return cls\n",
292+
"\n",
287293
"for code,msg in _httperrors:\n",
288294
" nm = f'HTTP{code}{msg.replace(\" \",\"\")}Error'\n",
289-
" cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)\n",
290-
" globals()[nm] = ExceptionsHTTP[code] = cls"
295+
" globals()[nm] = ExceptionsHTTP[code] = _mk_error(nm, code, msg)"
291296
]
292297
},
293298
{

0 commit comments

Comments
 (0)