-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathexceptions.py
More file actions
111 lines (84 loc) · 2.73 KB
/
exceptions.py
File metadata and controls
111 lines (84 loc) · 2.73 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
import time
class BadResponse(Exception):
"""
Currently used if the response can't be json encoded, despite a .json extension
"""
pass
class DeleteError(Exception):
"""
Used when a delete request did not return a 204
"""
pass
class Timeout(Exception):
"""
Used when a timeout occurs.
"""
pass
class RateLimited(Exception):
"""
Used when the Fitbit API rate limit has been exceeded and a request would cause an HTTP 429 error.
"""
def __init__(self, rate_limit_limit, rate_limit_remaining, rate_limit_reset):
self.rate_limit_limit = rate_limit_limit
self.rate_limit_remaining = rate_limit_remaining
self.rate_limit_reset = rate_limit_reset
super(RateLimited, self).__init__(
"Rate limit of {} requests exhausted. Reset in {:0f} seconds".format(rate_limit_limit,
rate_limit_reset - time.time()))
class HTTPException(Exception):
def __init__(self, response, *args, **kwargs):
try:
errors = json.loads(response.content.decode('utf8'))['errors']
message = '\n'.join([error['message'] for error in errors])
except Exception:
if hasattr(response, 'status_code') and response.status_code == 401:
message = response.content.decode('utf8')
else:
message = response
super(HTTPException, self).__init__(message, *args, **kwargs)
class HTTPBadRequest(HTTPException):
"""Generic >= 400 error
"""
pass
class HTTPUnauthorized(HTTPException):
"""401
"""
pass
class HTTPForbidden(HTTPException):
"""403
"""
pass
class HTTPNotFound(HTTPException):
"""404
"""
pass
class HTTPConflict(HTTPException):
"""409 - returned when creating conflicting resources
"""
pass
class HTTPTooManyRequests(HTTPException):
"""429 - returned when exceeding rate limits
"""
pass
class HTTPServerError(HTTPException):
"""Generic >= 500 error
"""
pass
def detect_and_raise_error(response):
if response.status_code == 401:
raise HTTPUnauthorized(response)
elif response.status_code == 403:
raise HTTPForbidden(response)
elif response.status_code == 404:
raise HTTPNotFound(response)
elif response.status_code == 409:
raise HTTPConflict(response)
elif response.status_code == 429:
exc = HTTPTooManyRequests(response)
exc.retry_after_secs = int(response.headers['Retry-After'])
raise exc
elif response.status_code >= 500:
raise HTTPServerError(response)
elif response.status_code >= 400:
raise HTTPBadRequest(response)