exceptions.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """Custom Exceptions."""
  2. from typing import Any, NoReturn
  3. class ReflexError(Exception):
  4. """Base exception for all Reflex exceptions."""
  5. class ConfigError(ReflexError):
  6. """Custom exception for config related errors."""
  7. class InvalidStateManagerMode(ReflexError, ValueError):
  8. """Raised when an invalid state manager mode is provided."""
  9. class ReflexRuntimeError(ReflexError, RuntimeError):
  10. """Custom RuntimeError for Reflex."""
  11. class UploadTypeError(ReflexError, TypeError):
  12. """Custom TypeError for upload related errors."""
  13. class EnvVarValueError(ReflexError, ValueError):
  14. """Custom ValueError raised when unable to convert env var to expected type."""
  15. class ComponentTypeError(ReflexError, TypeError):
  16. """Custom TypeError for component related errors."""
  17. class ChildrenTypeError(ComponentTypeError):
  18. """Raised when the children prop of a component is not a valid type."""
  19. def __init__(self, component: str, child: Any):
  20. """Initialize the exception.
  21. Args:
  22. component: The name of the component.
  23. child: The child that caused the error.
  24. """
  25. super().__init__(
  26. f"Component {component} received child {child} of type {type(child)}. "
  27. "Accepted types are other components, state vars, or primitive Python types (dict excluded)."
  28. )
  29. class EventHandlerTypeError(ReflexError, TypeError):
  30. """Custom TypeError for event handler related errors."""
  31. class EventHandlerValueError(ReflexError, ValueError):
  32. """Custom ValueError for event handler related errors."""
  33. class StateValueError(ReflexError, ValueError):
  34. """Custom ValueError for state related errors."""
  35. class VarNameError(ReflexError, NameError):
  36. """Custom NameError for when a state var has been shadowed by a substate var."""
  37. class VarTypeError(ReflexError, TypeError):
  38. """Custom TypeError for var related errors."""
  39. class VarValueError(ReflexError, ValueError):
  40. """Custom ValueError for var related errors."""
  41. class VarAttributeError(ReflexError, AttributeError):
  42. """Custom AttributeError for var related errors."""
  43. class UploadValueError(ReflexError, ValueError):
  44. """Custom ValueError for upload related errors."""
  45. class PageValueError(ReflexError, ValueError):
  46. """Custom ValueError for page related errors."""
  47. class RouteValueError(ReflexError, ValueError):
  48. """Custom ValueError for route related errors."""
  49. class VarOperationTypeError(ReflexError, TypeError):
  50. """Custom TypeError for when unsupported operations are performed on vars."""
  51. class VarDependencyError(ReflexError, ValueError):
  52. """Custom ValueError for when a var depends on a non-existent var."""
  53. class InvalidStylePropError(ReflexError, TypeError):
  54. """Custom Type Error when style props have invalid values."""
  55. class ImmutableStateError(ReflexError):
  56. """Raised when a background task attempts to modify state outside of context."""
  57. class LockExpiredError(ReflexError):
  58. """Raised when the state lock expires while an event is being processed."""
  59. class MatchTypeError(ReflexError, TypeError):
  60. """Raised when the return types of match cases are different."""
  61. class EventHandlerArgTypeMismatch(ReflexError, TypeError):
  62. """Raised when the annotations of args accepted by an EventHandler differs from the spec of the event trigger."""
  63. class EventFnArgMismatch(ReflexError, TypeError):
  64. """Raised when the number of args required by an event handler is more than provided by the event trigger."""
  65. class DynamicRouteArgShadowsStateVar(ReflexError, NameError):
  66. """Raised when a dynamic route arg shadows a state var."""
  67. class ComputedVarShadowsStateVar(ReflexError, NameError):
  68. """Raised when a computed var shadows a state var."""
  69. class ComputedVarShadowsBaseVars(ReflexError, NameError):
  70. """Raised when a computed var shadows a base var."""
  71. class EventHandlerShadowsBuiltInStateMethod(ReflexError, NameError):
  72. """Raised when an event handler shadows a built-in state method."""
  73. class GeneratedCodeHasNoFunctionDefs(ReflexError):
  74. """Raised when refactored code generated with flexgen has no functions defined."""
  75. class PrimitiveUnserializableToJSON(ReflexError, ValueError):
  76. """Raised when a primitive type is unserializable to JSON. Usually with NaN and Infinity."""
  77. class InvalidLifespanTaskType(ReflexError, TypeError):
  78. """Raised when an invalid task type is registered as a lifespan task."""
  79. class DynamicComponentMissingLibrary(ReflexError, ValueError):
  80. """Raised when a dynamic component is missing a library."""
  81. class SetUndefinedStateVarError(ReflexError, AttributeError):
  82. """Raised when setting the value of a var without first declaring it."""
  83. class StateSchemaMismatchError(ReflexError, TypeError):
  84. """Raised when the serialized schema of a state class does not match the current schema."""
  85. class EnvironmentVarValueError(ReflexError, ValueError):
  86. """Raised when an environment variable is set to an invalid value."""
  87. class DynamicComponentInvalidSignature(ReflexError, TypeError):
  88. """Raised when a dynamic component has an invalid signature."""
  89. class InvalidPropValueError(ReflexError):
  90. """Raised when a prop value is invalid."""
  91. class StateTooLargeError(ReflexError):
  92. """Raised when the state is too large to be serialized."""
  93. class StateSerializationError(ReflexError):
  94. """Raised when the state cannot be serialized."""
  95. class StateMismatchError(ReflexError, ValueError):
  96. """Raised when the state retrieved does not match the expected state."""
  97. class SystemPackageMissingError(ReflexError):
  98. """Raised when a system package is missing."""
  99. class EventDeserializationError(ReflexError, ValueError):
  100. """Raised when an event cannot be deserialized."""
  101. def raise_system_package_missing_error(package: str) -> NoReturn:
  102. """Raise a SystemPackageMissingError.
  103. Args:
  104. package: The name of the missing system package.
  105. Raises:
  106. SystemPackageMissingError: The raised exception.
  107. """
  108. from reflex.constants import IS_MACOS
  109. raise SystemPackageMissingError(
  110. f"System package '{package}' is missing."
  111. " Please install it through your system package manager."
  112. + (f" You can do so by running 'brew install {package}'." if IS_MACOS else "")
  113. )
  114. class InvalidLockWarningThresholdError(ReflexError):
  115. """Raised when an invalid lock warning threshold is provided."""
  116. class UnretrievableVarValueError(ReflexError):
  117. """Raised when the value of a var is not retrievable."""