1
0

exceptions.py 7.7 KB

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