-
Notifications
You must be signed in to change notification settings - Fork 372
/
exceptions.py
77 lines (50 loc) · 1.42 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
class VaultError(Exception):
def __init__(
self, message=None, errors=None, method=None, url=None, text=None, json=None
):
if errors:
message = ", ".join(errors)
self.errors = errors
self.method = method
self.url = url
self.text = text
self.json = json
super().__init__(message)
def __str__(self):
return f"{self.args[0]}, on {self.method} {self.url}"
@classmethod
def from_status(cls, status_code: int, *args, **kwargs):
_STATUS_EXCEPTION_MAP = {
400: InvalidRequest,
401: Unauthorized,
403: Forbidden,
404: InvalidPath,
429: RateLimitExceeded,
500: InternalServerError,
501: VaultNotInitialized,
502: BadGateway,
503: VaultDown,
}
return _STATUS_EXCEPTION_MAP.get(status_code, UnexpectedError)(*args, **kwargs)
class InvalidRequest(VaultError):
pass
class Unauthorized(VaultError):
pass
class Forbidden(VaultError):
pass
class InvalidPath(VaultError):
pass
class RateLimitExceeded(VaultError):
pass
class InternalServerError(VaultError):
pass
class VaultNotInitialized(VaultError):
pass
class VaultDown(VaultError):
pass
class UnexpectedError(VaultError):
pass
class BadGateway(VaultError):
pass
class ParamValidationError(VaultError):
pass