Use dict for HTTP status code → exception mapping.
This commit is contained in:
@@ -101,3 +101,36 @@ class MethodNotAllowed(ClientError):
|
||||
|
||||
def allowed_methods(self):
|
||||
return self.response['Allow']
|
||||
|
||||
|
||||
_exception_map = {
|
||||
301: Redirection,
|
||||
302: Redirection,
|
||||
303: Redirection,
|
||||
307: Redirection,
|
||||
400: BadRequest,
|
||||
401: UnauthorizedAccess,
|
||||
403: ForbiddenAccess,
|
||||
404: ResourceNotFound,
|
||||
405: MethodNotAllowed,
|
||||
409: ResourceConflict,
|
||||
410: ResourceGone,
|
||||
412: PreconditionFailed,
|
||||
422: ResourceInvalid,
|
||||
}
|
||||
|
||||
|
||||
def exception_for_status(status_code):
|
||||
"""Returns the exception class for the given status code."""
|
||||
|
||||
try:
|
||||
return _exception_map[status_code]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
if 400 <= status_code <= 499:
|
||||
return ClientError
|
||||
elif 500 <= status_code <= 599:
|
||||
return ServerError
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user