Completed
Push — master ( 4d0ec5...ba9245 )
by Ramon
12s
created

jsons.exceptions.JsonsError.message()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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