-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.py
More file actions
71 lines (51 loc) · 1.59 KB
/
errors.py
File metadata and controls
71 lines (51 loc) · 1.59 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
from typing import TypedDict, Literal
MethodErrorTypesLiterals = Literal[
'INVALID_AUTHORIZATION',
'INVALID_REQUEST',
'API_ERROR'
]
class MethodErrorOpts(TypedDict):
type: MethodErrorTypesLiterals
sub_type: str
message: str
code: str
class MethodError(BaseException):
type: MethodErrorTypesLiterals
sub_type: str
message: str
code: str
def __init__(self, opts: MethodErrorOpts):
super(MethodError, self).__init__()
self.type = opts.get('type', 'API_ERROR')
self.sub_type = opts.get('sub_type', '')
self.message = opts.get('message', '')
self.code = opts.get('code', '')
@staticmethod
def catch(fn):
def wrapper(*args, **kwargs):
res = fn(*args, **kwargs)
if (res is not None) and ('error' in res) and ('id' not in res):
raise MethodError.generate(res['error'])
return res
return wrapper
@staticmethod
def generate(opts: MethodErrorOpts):
error_type = opts.get('type')
if error_type == 'INVALID_AUTHORIZATION':
return MethodAuthorizationError(opts)
if error_type == 'INVALID_REQUEST':
return MethodInvalidRequestError(opts)
if error_type == 'API_ERROR':
return MethodInternalError(opts)
return MethodError(opts)
class MethodInternalError(MethodError):
pass
class MethodInvalidRequestError(MethodError):
pass
class MethodAuthorizationError(MethodError):
pass
class ResourceError(TypedDict):
type: str
sub_type: str
message: str
code: str