Completed
Push — master ( 87b7dd...8c4489 )
by Ramon
12s
created

jsons.exceptions.DeserializationError.__init__()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 4
dl 0
loc 10
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
6
7
class JsonsError(Exception):
8
    """
9
    Base class for all `jsons` errors.
10
    """
11
    def __init__(self, message: str):
12
        """
13
        Constructor.
14
        :param message: the message describing the problem.
15
        """
16
        Exception.__init__(self, message)
17
18
19
class ArgumentError(JsonsError, ValueError):
20
    """
21
    Raised when serialization or deserialization went wrong caused by a wrong
22
    argument when serializing or deserializing.
23
    """
24
    def __init__(self, message: str, argument: str):
25
        """
26
        Constructor.
27
        :param message: the message describing the problem.
28
        :param argument: the name of the argument in question.
29
        """
30
        JsonsError.__init__(self, message)
31
        ValueError.__init__(self, message)
32
        self._argument = argument
33
34
    @property
35
    def argument(self) -> str:
36
        """
37
        The argument in question.
38
        :return: the name of the argument.
39
        """
40
        return self._argument
41
42
43
class DeserializationError(JsonsError):
44
    """
45
    Raised when deserialization failed for some reason.
46
    """
47
    def __init__(self, message: str, source: object, target: type):
48
        """
49
        Constructor.
50
        :param message: the message describing the problem.
51
        :param source: the object that was to be deserialized.
52
        :param target: the type to which `source` was to be deserialized.
53
        """
54
        JsonsError.__init__(self, message)
55
        self._source = source
56
        self._target = target
57
58
    @property
59
    def source(self) -> object:
60
        """
61
        The object that was to be deserialized.
62
        :return: the object that was to be deserialized.
63
        """
64
        return self._source
65
66
    @property
67
    def target(self) -> type:
68
        """
69
        The target type to which `source` was to be deserialized.
70
        :return: the type to which `source` was to be deserialized.
71
        """
72
        return self._target
73
74
75
class SerializationError(JsonsError):
76
    """
77
    Raised when serialization failed for some reason.
78
    """
79
80
81
class DecodeError(DeserializationError, JSONDecodeError):
82
    """
83
    Raised when decoding a string or bytes to Python types failed. This error
84
    is actually a wrapper around `json.JSONDecodeError`.
85
    """
86
    def __init__(self, message: str, source: object, target: type,
87
                 error: JSONDecodeError):
88
        """
89
        Constructor.
90
        :param message: the message of this error.
91
        :param source: the object that was to be deserialized.
92
        :param target: the type to which `source` was to be deserialized.
93
        :param error: the wrapped `JSONDecodeError`.
94
        """
95
        DeserializationError.__init__(self, message, source, target)
96
        JSONDecodeError.__init__(self, message, error.doc, error.pos)
97
98
99
class UnfulfilledArgumentError(DeserializationError, ArgumentError):
100
    """
101
    Raised on a deserialization failure when an argument could not be fulfilled
102
    by the given object attr_getter.
103
    """
104
    def __init__(self,
105
                 message: str,
106
                 argument: str,
107
                 source: object,
108
                 target: type):
109
        """
110
        Constructor.
111
        :param message: the message of this error.
112
        :param argument: the argument that was unfulfilled.
113
        :param source: the object that was to be deserialized.
114
        :param target: the type to which `source` was to be deserialized.
115
        """
116
        DeserializationError.__init__(self, message, source, target)
117
        ArgumentError.__init__(self, message, argument)
118
119
120
class SignatureMismatchError(DeserializationError, ArgumentError):
121
    def __init__(self,
122
                 message: str,
123
                 argument: str,
124
                 source: object,
125
                 target: type):
126
        DeserializationError.__init__(self, message, source, target)
127
        ArgumentError.__init__(self, message, argument)
128
129
130
class InvalidDecorationError(JsonsError):
131
    """
132
    Raised when a jsons decorator was wrongly used.
133
    """
134
    def __init__(self, message: str):
135
        """
136
        Constructor.
137
        :param message: the message of this error.
138
        """
139
        JsonsError.__init__(self, message)
140