Use dict for HTTP status code → exception mapping.

This commit is contained in:
2016-07-08 12:31:21 +02:00
parent d76fcfdeba
commit 8565bf37b2
2 changed files with 41 additions and 28 deletions

View File

@@ -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