exceptions.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. """Custom Exceptions."""
  2. from typing import Any
  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 UntypedComputedVarError(ReflexError, TypeError):
  44. """Custom TypeError for untyped computed var errors."""
  45. def __init__(self, var_name):
  46. """Initialize the UntypedComputedVarError.
  47. Args:
  48. var_name: The name of the computed var.
  49. """
  50. super().__init__(f"Computed var '{var_name}' must have a type annotation.")
  51. class MissingAnnotationError(ReflexError, TypeError):
  52. """Custom TypeError for missing annotations."""
  53. def __init__(self, var_name):
  54. """Initialize the MissingAnnotationError.
  55. Args:
  56. var_name: The name of the var.
  57. """
  58. super().__init__(f"Var '{var_name}' must have a type annotation.")
  59. class UploadValueError(ReflexError, ValueError):
  60. """Custom ValueError for upload related errors."""
  61. class PageValueError(ReflexError, ValueError):
  62. """Custom ValueError for page related errors."""
  63. class RouteValueError(ReflexError, ValueError):
  64. """Custom ValueError for route related errors."""
  65. class VarOperationTypeError(ReflexError, TypeError):
  66. """Custom TypeError for when unsupported operations are performed on vars."""
  67. class VarDependencyError(ReflexError, ValueError):
  68. """Custom ValueError for when a var depends on a non-existent var."""
  69. class InvalidStylePropError(ReflexError, TypeError):
  70. """Custom Type Error when style props have invalid values."""
  71. class ImmutableStateError(ReflexError):
  72. """Raised when a background task attempts to modify state outside of context."""
  73. class LockExpiredError(ReflexError):
  74. """Raised when the state lock expires while an event is being processed."""
  75. class MatchTypeError(ReflexError, TypeError):
  76. """Raised when the return types of match cases are different."""
  77. class EventHandlerArgTypeMismatchError(ReflexError, TypeError):
  78. """Raised when the annotations of args accepted by an EventHandler differs from the spec of the event trigger."""
  79. class EventFnArgMismatchError(ReflexError, TypeError):
  80. """Raised when the number of args required by an event handler is more than provided by the event trigger."""
  81. class DynamicRouteArgShadowsStateVar(ReflexError, NameError):
  82. """Raised when a dynamic route arg shadows a state var."""
  83. class ComputedVarShadowsStateVar(ReflexError, NameError):
  84. """Raised when a computed var shadows a state var."""
  85. class ComputedVarShadowsBaseVars(ReflexError, NameError):
  86. """Raised when a computed var shadows a base var."""
  87. class EventHandlerShadowsBuiltInStateMethod(ReflexError, NameError):
  88. """Raised when an event handler shadows a built-in state method."""
  89. class GeneratedCodeHasNoFunctionDefs(ReflexError):
  90. """Raised when refactored code generated with flexgen has no functions defined."""
  91. class PrimitiveUnserializableToJSON(ReflexError, ValueError):
  92. """Raised when a primitive type is unserializable to JSON. Usually with NaN and Infinity."""
  93. class InvalidLifespanTaskType(ReflexError, TypeError):
  94. """Raised when an invalid task type is registered as a lifespan task."""
  95. class DynamicComponentMissingLibrary(ReflexError, ValueError):
  96. """Raised when a dynamic component is missing a library."""
  97. class SetUndefinedStateVarError(ReflexError, AttributeError):
  98. """Raised when setting the value of a var without first declaring it."""
  99. class StateSchemaMismatchError(ReflexError, TypeError):
  100. """Raised when the serialized schema of a state class does not match the current schema."""
  101. class EnvironmentVarValueError(ReflexError, ValueError):
  102. """Raised when an environment variable is set to an invalid value."""
  103. class DynamicComponentInvalidSignature(ReflexError, TypeError):
  104. """Raised when a dynamic component has an invalid signature."""
  105. class InvalidPropValueError(ReflexError):
  106. """Raised when a prop value is invalid."""
  107. class StateTooLargeError(ReflexError):
  108. """Raised when the state is too large to be serialized."""
  109. class StateSerializationError(ReflexError):
  110. """Raised when the state cannot be serialized."""
  111. class StateMismatchError(ReflexError, ValueError):
  112. """Raised when the state retrieved does not match the expected state."""
  113. class SystemPackageMissingError(ReflexError):
  114. """Raised when a system package is missing."""
  115. def __init__(self, package: str):
  116. """Initialize the SystemPackageMissingError.
  117. Args:
  118. package: The missing package.
  119. """
  120. from reflex.constants import IS_MACOS
  121. extra = (
  122. f" You can do so by running 'brew install {package}'." if IS_MACOS else ""
  123. )
  124. super().__init__(
  125. f"System package '{package}' is missing."
  126. f" Please install it through your system package manager.{extra}"
  127. )
  128. class EventDeserializationError(ReflexError, ValueError):
  129. """Raised when an event cannot be deserialized."""
  130. class InvalidLockWarningThresholdError(ReflexError):
  131. """Raised when an invalid lock warning threshold is provided."""
  132. class UnretrievableVarValueError(ReflexError):
  133. """Raised when the value of a var is not retrievable."""