-
Notifications
You must be signed in to change notification settings - Fork 101
/
exceptions.py
97 lines (66 loc) · 1.75 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import json
class HTTPError(Exception):
""" Base of all other errors"""
def __init__(self, *args):
if len(args) == 4:
self.status_code = args[0]
self.reason = args[1]
self.body = args[2]
self.headers = args[3]
else:
self.status_code = args[0].code
self.reason = args[0].reason
self.body = args[0].read()
self.headers = args[0].hdrs
def __reduce__(self):
return (
HTTPError,
(self.status_code, self.reason, self.body, self.headers)
)
@property
def to_dict(self):
"""
:return: dict of response error from the API
"""
return json.loads(self.body.decode('utf-8'))
class BadRequestsError(HTTPError):
pass
class UnauthorizedError(HTTPError):
pass
class ForbiddenError(HTTPError):
pass
class NotFoundError(HTTPError):
pass
class MethodNotAllowedError(HTTPError):
pass
class PayloadTooLargeError(HTTPError):
pass
class UnsupportedMediaTypeError(HTTPError):
pass
class TooManyRequestsError(HTTPError):
pass
class InternalServerError(HTTPError):
pass
class ServiceUnavailableError(HTTPError):
pass
class GatewayTimeoutError(HTTPError):
pass
err_dict = {
400: BadRequestsError,
401: UnauthorizedError,
403: ForbiddenError,
404: NotFoundError,
405: MethodNotAllowedError,
413: PayloadTooLargeError,
415: UnsupportedMediaTypeError,
429: TooManyRequestsError,
500: InternalServerError,
503: ServiceUnavailableError,
504: GatewayTimeoutError
}
def handle_error(error):
try:
exc = err_dict[error.code](error)
except KeyError:
return HTTPError(error)
return exc