exceptions.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. class RLPException(Exception):
  2. """Base class for exceptions raised by this package."""
  3. class EncodingError(RLPException):
  4. """
  5. Exception raised if encoding fails.
  6. :ivar obj: the object that could not be encoded
  7. """
  8. def __init__(self, message, obj):
  9. super().__init__(message)
  10. self.obj = obj
  11. class DecodingError(RLPException):
  12. """
  13. Exception raised if decoding fails.
  14. :ivar rlp: the RLP string that could not be decoded
  15. """
  16. def __init__(self, message, rlp):
  17. super().__init__(message)
  18. self.rlp = rlp
  19. class SerializationError(RLPException):
  20. """
  21. Exception raised if serialization fails.
  22. :ivar obj: the object that could not be serialized
  23. """
  24. def __init__(self, message, obj):
  25. super().__init__(message)
  26. self.obj = obj
  27. class ListSerializationError(SerializationError):
  28. """
  29. Exception raised if serialization by a :class:`sedes.List` fails.
  30. :ivar element_exception: the exception that occurred during the serialization of one
  31. of the elements, or `None` if the error is unrelated to a
  32. specific element
  33. :ivar index: the index in the list that produced the error or `None` if the error is
  34. unrelated to a specific element
  35. """
  36. def __init__(self, message=None, obj=None, element_exception=None, index=None):
  37. if message is None:
  38. assert index is not None
  39. assert element_exception is not None
  40. message = (
  41. f"Serialization failed because of element at index {index} "
  42. f'("{str(element_exception)}")'
  43. )
  44. super().__init__(message, obj)
  45. self.index = index
  46. self.element_exception = element_exception
  47. class ObjectSerializationError(SerializationError):
  48. """
  49. Exception raised if serialization of a :class:`sedes.Serializable` object fails.
  50. :ivar sedes: the :class:`sedes.Serializable` that failed
  51. :ivar list_exception: exception raised by the underlying list sedes, or `None` if no
  52. such exception has been raised
  53. :ivar field: name of the field of the object that produced the error, or `None` if
  54. no field responsible for the error
  55. """
  56. def __init__(self, message=None, obj=None, sedes=None, list_exception=None):
  57. if message is None:
  58. assert list_exception is not None
  59. if list_exception.element_exception is None:
  60. field = None
  61. message = (
  62. "Serialization failed because of underlying list "
  63. f'("{str(list_exception)}")'
  64. )
  65. else:
  66. assert sedes is not None
  67. field = sedes._meta.field_names[list_exception.index]
  68. message = (
  69. f"Serialization failed because of field {field} "
  70. f'("{str(list_exception.element_exception)}")'
  71. )
  72. else:
  73. field = None
  74. super().__init__(message, obj)
  75. self.field = field
  76. self.list_exception = list_exception
  77. class DeserializationError(RLPException):
  78. """
  79. Exception raised if deserialization fails.
  80. :ivar serial: the decoded RLP string that could not be deserialized
  81. """
  82. def __init__(self, message, serial):
  83. super().__init__(message)
  84. self.serial = serial
  85. class ListDeserializationError(DeserializationError):
  86. """
  87. Exception raised if deserialization by a :class:`sedes.List` fails.
  88. :ivar element_exception: the exception that occurred during the deserialization of
  89. one of the elements, or `None` if the error is unrelated to
  90. a specific element
  91. :ivar index: the index in the list that produced the error or `None` if the error is
  92. unrelated to a specific element
  93. """
  94. def __init__(self, message=None, serial=None, element_exception=None, index=None):
  95. if not message:
  96. assert index is not None
  97. assert element_exception is not None
  98. message = (
  99. f"Deserialization failed because of element at index {index} "
  100. f'("{str(element_exception)}")'
  101. )
  102. super().__init__(message, serial)
  103. self.index = index
  104. self.element_exception = element_exception
  105. class ObjectDeserializationError(DeserializationError):
  106. """
  107. Exception raised if deserialization by a :class:`sedes.Serializable` fails.
  108. :ivar sedes: the :class:`sedes.Serializable` that failed
  109. :ivar list_exception: exception raised by the underlying list sedes, or `None` if no
  110. such exception has been raised
  111. :ivar field: name of the field of the object that produced the error, or `None` if
  112. no field responsible for the error
  113. """
  114. def __init__(self, message=None, serial=None, sedes=None, list_exception=None):
  115. if not message:
  116. assert list_exception is not None
  117. if list_exception.element_exception is None:
  118. field = None
  119. message = (
  120. "Deserialization failed because of underlying list "
  121. f'("{str(list_exception)}")'
  122. )
  123. else:
  124. assert sedes is not None
  125. field = sedes._meta.field_names[list_exception.index]
  126. message = (
  127. f"Deserialization failed because of field {field} "
  128. f'("{str(list_exception.element_exception)}")'
  129. )
  130. super().__init__(message, serial)
  131. self.sedes = sedes
  132. self.list_exception = list_exception
  133. self.field = field