This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 411
/
exceptions.py
74 lines (57 loc) · 1.87 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
from collections import namedtuple
Position = namedtuple("Position", ["line_no", "column_no", "index"])
class ErrorMessage:
def __init__(self, text, code, index=None, position=None):
self.text = text
self.code = code
self.index = index
self.position = position
def __eq__(self, other):
return (
self.text == other.text
and self.code == other.code
and self.index == other.index
and self.position == other.position
)
def __repr__(self):
return "%s(%s, code=%s, index=%s, position=%s)" % (
self.__class__.__name__,
repr(self.text),
repr(self.code),
repr(self.index),
repr(self.position),
)
class DecodeError(Exception):
def __init__(self, messages, summary=None):
self.messages = messages
self.summary = summary
super().__init__(messages)
class ParseError(DecodeError):
pass
class ValidationError(DecodeError):
def as_dict(self):
ret = {}
for message in self.messages:
lookup = ret
if message.index:
for key in message.index[:-1]:
lookup.setdefault(key, {})
lookup = lookup[key]
key = message.index[-1] if message.index else None
lookup[key] = message.text
return ret
class ErrorResponse(Exception):
"""
Raised when a client request results in an error response being returned.
"""
def __init__(self, title, status_code, content):
self.title = title
self.status_code = status_code
self.content = content
class ClientError(Exception):
"""
Raised when a client is unable to fulfil an API request.
"""
def __init__(self, messages):
self.messages = messages
super().__init__(messages)