1
|
|
|
""" |
2
|
|
|
Contains the classes that may be raised by jsons. |
3
|
|
|
""" |
4
|
|
|
from json import JSONDecodeError |
5
|
|
|
from typing import Optional |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class JsonsError(Exception): |
9
|
|
|
""" |
10
|
|
|
Base class for all `jsons` errors. |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
def __init__(self, message: str): |
14
|
|
|
""" |
15
|
|
|
Constructor. |
16
|
|
|
:param message: the message describing the problem. |
17
|
|
|
""" |
18
|
|
|
Exception.__init__(self, message) |
19
|
|
|
self._message = message |
20
|
|
|
|
21
|
|
|
@property |
22
|
|
|
def message(self): |
23
|
|
|
return self._message |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class ValidationError(JsonsError): |
27
|
|
|
""" |
28
|
|
|
Raised when the validation of an object failed. |
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class ArgumentError(JsonsError, ValueError): |
33
|
|
|
""" |
34
|
|
|
Raised when serialization or deserialization went wrong caused by a wrong |
35
|
|
|
argument when serializing or deserializing. |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
def __init__(self, message: str, argument: str): |
39
|
|
|
""" |
40
|
|
|
Constructor. |
41
|
|
|
:param message: the message describing the problem. |
42
|
|
|
:param argument: the name of the argument in question. |
43
|
|
|
""" |
44
|
|
|
JsonsError.__init__(self, message) |
45
|
|
|
ValueError.__init__(self, message) |
46
|
|
|
self._argument = argument |
47
|
|
|
|
48
|
|
|
@property |
49
|
|
|
def argument(self) -> str: |
50
|
|
|
""" |
51
|
|
|
The argument in question. |
52
|
|
|
:return: the name of the argument. |
53
|
|
|
""" |
54
|
|
|
return self._argument |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class DeserializationError(JsonsError): |
58
|
|
|
""" |
59
|
|
|
Raised when deserialization failed for some reason. |
60
|
|
|
""" |
61
|
|
|
|
62
|
|
|
def __init__(self, message: str, source: object, target: Optional[type]): |
63
|
|
|
""" |
64
|
|
|
Constructor. |
65
|
|
|
:param message: the message describing the problem. |
66
|
|
|
:param source: the object that was to be deserialized. |
67
|
|
|
:param target: the type to which `source` was to be deserialized. |
68
|
|
|
""" |
69
|
|
|
JsonsError.__init__(self, message) |
70
|
|
|
self._source = source |
71
|
|
|
self._target = target |
72
|
|
|
|
73
|
|
|
@property |
74
|
|
|
def source(self) -> object: |
75
|
|
|
""" |
76
|
|
|
The object that was to be deserialized. |
77
|
|
|
:return: the object that was to be deserialized. |
78
|
|
|
""" |
79
|
|
|
return self._source |
80
|
|
|
|
81
|
|
|
@property |
82
|
|
|
def target(self) -> Optional[type]: |
83
|
|
|
""" |
84
|
|
|
The target type to which `source` was to be deserialized. |
85
|
|
|
:return: the type to which `source` was to be deserialized. |
86
|
|
|
""" |
87
|
|
|
return self._target |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
class SerializationError(JsonsError): |
91
|
|
|
""" |
92
|
|
|
Raised when serialization failed for some reason. |
93
|
|
|
""" |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
class DecodeError(DeserializationError, JSONDecodeError): |
97
|
|
|
""" |
98
|
|
|
Raised when decoding a string or bytes to Python types failed. This error |
99
|
|
|
is actually a wrapper around `json.JSONDecodeError`. |
100
|
|
|
""" |
101
|
|
|
|
102
|
|
|
def __init__(self, message: str, source: object, target: type, |
103
|
|
|
error: JSONDecodeError): |
104
|
|
|
""" |
105
|
|
|
Constructor. |
106
|
|
|
:param message: the message of this error. |
107
|
|
|
:param source: the object that was to be deserialized. |
108
|
|
|
:param target: the type to which `source` was to be deserialized. |
109
|
|
|
:param error: the wrapped `JSONDecodeError`. |
110
|
|
|
""" |
111
|
|
|
DeserializationError.__init__(self, message, source, target) |
112
|
|
|
JSONDecodeError.__init__(self, message, error.doc, error.pos) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
class UnfulfilledArgumentError(DeserializationError, ArgumentError): |
116
|
|
|
""" |
117
|
|
|
Raised on a deserialization failure when an argument could not be fulfilled |
118
|
|
|
by the given object attr_getter. |
119
|
|
|
""" |
120
|
|
|
|
121
|
|
|
def __init__(self, |
122
|
|
|
message: str, |
123
|
|
|
argument: str, |
124
|
|
|
source: object, |
125
|
|
|
target: type): |
126
|
|
|
""" |
127
|
|
|
Constructor. |
128
|
|
|
:param message: the message of this error. |
129
|
|
|
:param argument: the argument that was unfulfilled. |
130
|
|
|
:param source: the object that was to be deserialized. |
131
|
|
|
:param target: the type to which `source` was to be deserialized. |
132
|
|
|
""" |
133
|
|
|
DeserializationError.__init__(self, message, source, target) |
134
|
|
|
ArgumentError.__init__(self, message, argument) |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
class SignatureMismatchError(DeserializationError, ArgumentError): |
138
|
|
|
""" |
139
|
|
|
Raised when the source could not be deserialized into the target type due |
140
|
|
|
to a mismatch between the source's attributes and the target's accepted |
141
|
|
|
parameters. This error is raised in "strict-mode" only. |
142
|
|
|
""" |
143
|
|
|
|
144
|
|
|
def __init__(self, |
145
|
|
|
message: str, |
146
|
|
|
argument: str, |
147
|
|
|
source: object, |
148
|
|
|
target: type): |
149
|
|
|
""" |
150
|
|
|
Constructor. |
151
|
|
|
:param message: the message of this error. |
152
|
|
|
:param argument: the argument that caused the problem. |
153
|
|
|
:param source: the object that was to be deserialized. |
154
|
|
|
:param target: the type to which `source` was to be deserialized. |
155
|
|
|
""" |
156
|
|
|
DeserializationError.__init__(self, message, source, target) |
157
|
|
|
ArgumentError.__init__(self, message, argument) |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
class UnknownClassError(DeserializationError): |
161
|
|
|
""" |
162
|
|
|
Raised when jsons failed to find a type instance to deserialize to. If this |
163
|
|
|
error occurs, consider using ``jsons.announce_class``. |
164
|
|
|
""" |
165
|
|
|
|
166
|
|
|
def __init__(self, message: str, source: object, target_name: str): |
167
|
|
|
""" |
168
|
|
|
Constructor. |
169
|
|
|
:param message: the message of this error. |
170
|
|
|
:param source: the object that was to be deserialized. |
171
|
|
|
:param target_name: the name of the type that was the target type. |
172
|
|
|
""" |
173
|
|
|
DeserializationError.__init__(self, message, source, None) |
174
|
|
|
self._target_name = target_name |
175
|
|
|
|
176
|
|
|
@property |
177
|
|
|
def target_name(self) -> str: |
178
|
|
|
""" |
179
|
|
|
The name of the type that was unsuccessfully attempted to deserialize |
180
|
|
|
into. |
181
|
|
|
:return: the name of the type that was to be the target type. |
182
|
|
|
""" |
183
|
|
|
return self._target_name |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
class InvalidDecorationError(JsonsError): |
187
|
|
|
""" |
188
|
|
|
Raised when a jsons decorator was wrongly used. |
189
|
|
|
""" |
190
|
|
|
|
191
|
|
|
def __init__(self, message: str): |
192
|
|
|
""" |
193
|
|
|
Constructor. |
194
|
|
|
:param message: the message of this error. |
195
|
|
|
""" |
196
|
|
|
JsonsError.__init__(self, message) |
197
|
|
|
|