exceptions.py 5.7 KB

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