exceptions.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import parsimonious
  2. class EncodingError(Exception):
  3. """
  4. Base exception for any error that occurs during encoding.
  5. """
  6. class EncodingTypeError(EncodingError):
  7. """
  8. Raised when trying to encode a python value whose type is not supported for
  9. the output ABI type.
  10. """
  11. class IllegalValue(EncodingError):
  12. """
  13. Raised when trying to encode a python value with the correct type but with
  14. a value that is not considered legal for the output ABI type.
  15. .. code-block:: python
  16. fixed128x19_encoder(Decimal('NaN')) # cannot encode NaN
  17. """
  18. class ValueOutOfBounds(IllegalValue):
  19. """
  20. Raised when trying to encode a python value with the correct type but with
  21. a value that appears outside the range of valid values for the output ABI
  22. type.
  23. .. code-block:: python
  24. ufixed8x1_encoder(Decimal('25.6')) # out of bounds
  25. """
  26. class DecodingError(Exception):
  27. """
  28. Base exception for any error that occurs during decoding.
  29. """
  30. class InsufficientDataBytes(DecodingError):
  31. """
  32. Raised when there are insufficient data to decode a value for a given ABI type.
  33. """
  34. class NonEmptyPaddingBytes(DecodingError):
  35. """
  36. Raised when the padding bytes of an ABI value are malformed.
  37. """
  38. class InvalidPointer(DecodingError):
  39. """
  40. Raised when the pointer to a value in the ABI encoding is invalid.
  41. """
  42. class ParseError(parsimonious.ParseError): # type: ignore[misc] # subclasses Any
  43. """
  44. Raised when an ABI type string cannot be parsed.
  45. """
  46. def __str__(self) -> str:
  47. return (
  48. f"Parse error at '{self.text[self.pos : self.pos + 5]}' "
  49. f"(column {self.column()}) in type string '{self.text}'"
  50. )
  51. class ABITypeError(ValueError):
  52. """
  53. Raised when a parsed ABI type has inconsistent properties; for example,
  54. when trying to parse the type string ``'uint7'`` (which has a bit-width
  55. that is not congruent with zero modulo eight).
  56. """
  57. class PredicateMappingError(Exception):
  58. """
  59. Raised when an error occurs in a registry's internal mapping.
  60. """
  61. class NoEntriesFound(ValueError, PredicateMappingError):
  62. """
  63. Raised when no registration is found for a type string in a registry's
  64. internal mapping.
  65. .. warning::
  66. In a future version of ``eth-abi``, this error class will no longer
  67. inherit from ``ValueError``.
  68. """
  69. class MultipleEntriesFound(ValueError, PredicateMappingError):
  70. """
  71. Raised when multiple registrations are found for a type string in a
  72. registry's internal mapping. This error is non-recoverable and indicates
  73. that a registry was configured incorrectly. Registrations are expected to
  74. cover completely distinct ranges of type strings.
  75. .. warning::
  76. In a future version of ``eth-abi``, this error class will no longer
  77. inherit from ``ValueError``.
  78. """