File: exceptions.py

package info (click to toggle)
pyrlp 0.5.1-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 304 kB
  • sloc: python: 1,717; makefile: 234; sh: 16
file content (144 lines) | stat: -rw-r--r-- 5,809 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class RLPException(Exception):
    """Base class for exceptions raised by this package."""
    pass


class EncodingError(RLPException):
    """Exception raised if encoding fails.

    :ivar obj: the object that could not be encoded
    """

    def __init__(self, message, obj):
        super(EncodingError, self).__init__(message)
        self.obj = obj


class DecodingError(RLPException):
    """Exception raised if decoding fails.

    :ivar rlp: the RLP string that could not be decoded
    """

    def __init__(self, message, rlp):
        super(DecodingError, self).__init__(message)
        self.rlp = rlp


class SerializationError(RLPException):
    """Exception raised if serialization fails.

    :ivar obj: the object that could not be serialized
    """

    def __init__(self, message, obj):
        super(SerializationError, self).__init__(message)
        self.obj = obj


class ListSerializationError(SerializationError):
    """Exception raised if serialization by a :class:`sedes.List` fails.

    :ivar element_exception: the exception that occurred during the serialization of one of the
                             elements, or `None` if the error is unrelated to a specific element
    :ivar index: the index in the list that produced the error or `None` if the error is unrelated
                 to a specific element
    """

    def __init__(self, message=None, obj=None, element_exception=None, index=None):
        if message is None:
            assert index is not None
            assert element_exception is not None
            message = ('Serialization failed because of element at index {} '
                       '("{}")'.format(index, str(element_exception)))
        super(ListSerializationError, self).__init__(message, obj)
        self.index = index
        self.element_exception = element_exception


class ObjectSerializationError(SerializationError):
    """Exception raised if serialization of a :class:`sedes.Serializable` object fails.

    :ivar sedes: the :class:`sedes.Serializable` that failed
    :ivar list_exception: exception raised by the underlying list sedes, or `None` if no such
                          exception has been raised
    :ivar field: name of the field of the object that produced the error, or `None` if no field
                 responsible for the error
    """

    def __init__(self, message=None, obj=None, sedes=None, list_exception=None):
        if message is None:
            assert list_exception is not None
            if list_exception.element_exception is None:
                field = None
                message = ('Serialization failed because of underlying list '
                           '("{}")'.format(str(list_exception)))
            else:
                assert sedes is not None
                field = sedes.fields[list_exception.index][0]
                message = ('Serialization failed because of field {} '
                           '("{}")'.format(field, str(list_exception.element_exception)))
        else:
            field = None
        super(ObjectSerializationError, self).__init__(message, obj)
        self.field = field
        self.list_exception = list_exception


class DeserializationError(RLPException):
    """Exception raised if deserialization fails.

    :ivar serial: the decoded RLP string that could not be deserialized
    """

    def __init__(self, message, serial):
        super(DeserializationError, self).__init__(message)
        self.serial = serial


class ListDeserializationError(DeserializationError):
    """Exception raised if deserialization by a :class:`sedes.List` fails.

    :ivar element_exception: the exception that occurred during the deserialization of one of the
                             elements, or `None` if the error is unrelated to a specific element
    :ivar index: the index in the list that produced the error or `None` if the error is unrelated
                 to a specific element
    """

    def __init__(self, message=None, serial=None, element_exception=None, index=None):
        if not message:
            assert index is not None
            assert element_exception is not None
            message = ('Deserialization failed because of element at index {} '
                       '("{}")'.format(index, str(element_exception)))
        super(ListDeserializationError, self).__init__(message, serial)
        self.index = index
        self.element_exception = element_exception


class ObjectDeserializationError(DeserializationError):
    """Exception raised if deserialization by a :class:`sedes.Serializable` fails.

    :ivar sedes: the :class:`sedes.Serializable` that failed
    :ivar list_exception: exception raised by the underlying list sedes, or `None` if no such
                          exception has been raised
    :ivar field: name of the field of the object that produced the error, or `None` if no field
                 responsible for the error
    """

    def __init__(self, message=None, serial=None, sedes=None, list_exception=None):
        if not message:
            assert list_exception is not None
            if list_exception.element_exception is None:
                field = None
                message = ('Deserialization failed because of underlying list '
                           '("{}")'.format(str(list_exception)))
            else:
                assert sedes is not None
                field = sedes.fields[list_exception.index][0]
                message = ('Deserialization failed because of field {} '
                           '("{}")'.format(field, str(list_exception.element_exception)))
        super(ObjectDeserializationError, self).__init__(message, serial)
        self.sedes = sedes
        self.list_exception = list_exception
        self.field = field