Completed
Pull Request — master (#23)
by Ramon
01:34
created

jsons.exceptions.JsonsError.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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