exceptions.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """Custom Exceptions."""
  2. from typing import 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 EventHandlerTypeError(ReflexError, TypeError):
  18. """Custom TypeError for event handler related errors."""
  19. class EventHandlerValueError(ReflexError, ValueError):
  20. """Custom ValueError for event handler related errors."""
  21. class StateValueError(ReflexError, ValueError):
  22. """Custom ValueError for state related errors."""
  23. class VarNameError(ReflexError, NameError):
  24. """Custom NameError for when a state var has been shadowed by a substate var."""
  25. class VarTypeError(ReflexError, TypeError):
  26. """Custom TypeError for var related errors."""
  27. class VarValueError(ReflexError, ValueError):
  28. """Custom ValueError for var related errors."""
  29. class VarAttributeError(ReflexError, AttributeError):
  30. """Custom AttributeError for var related errors."""
  31. class UploadValueError(ReflexError, ValueError):
  32. """Custom ValueError for upload related errors."""
  33. class PageValueError(ReflexError, ValueError):
  34. """Custom ValueError for page related errors."""
  35. class RouteValueError(ReflexError, ValueError):
  36. """Custom ValueError for route related errors."""
  37. class VarOperationTypeError(ReflexError, TypeError):
  38. """Custom TypeError for when unsupported operations are performed on vars."""
  39. class VarDependencyError(ReflexError, ValueError):
  40. """Custom ValueError for when a var depends on a non-existent var."""
  41. class InvalidStylePropError(ReflexError, TypeError):
  42. """Custom Type Error when style props have invalid values."""
  43. class ImmutableStateError(ReflexError):
  44. """Raised when a background task attempts to modify state outside of context."""
  45. class LockExpiredError(ReflexError):
  46. """Raised when the state lock expires while an event is being processed."""
  47. class MatchTypeError(ReflexError, TypeError):
  48. """Raised when the return types of match cases are different."""
  49. class EventHandlerArgTypeMismatch(ReflexError, TypeError):
  50. """Raised when the annotations of args accepted by an EventHandler differs from the spec of the event trigger."""
  51. class EventFnArgMismatch(ReflexError, TypeError):
  52. """Raised when the number of args required by an event handler is more than provided by the event trigger."""
  53. class DynamicRouteArgShadowsStateVar(ReflexError, NameError):
  54. """Raised when a dynamic route arg shadows a state var."""
  55. class ComputedVarShadowsStateVar(ReflexError, NameError):
  56. """Raised when a computed var shadows a state var."""
  57. class ComputedVarShadowsBaseVars(ReflexError, NameError):
  58. """Raised when a computed var shadows a base var."""
  59. class EventHandlerShadowsBuiltInStateMethod(ReflexError, NameError):
  60. """Raised when an event handler shadows a built-in state method."""
  61. class GeneratedCodeHasNoFunctionDefs(ReflexError):
  62. """Raised when refactored code generated with flexgen has no functions defined."""
  63. class PrimitiveUnserializableToJSON(ReflexError, ValueError):
  64. """Raised when a primitive type is unserializable to JSON. Usually with NaN and Infinity."""
  65. class InvalidLifespanTaskType(ReflexError, TypeError):
  66. """Raised when an invalid task type is registered as a lifespan task."""
  67. class DynamicComponentMissingLibrary(ReflexError, ValueError):
  68. """Raised when a dynamic component is missing a library."""
  69. class SetUndefinedStateVarError(ReflexError, AttributeError):
  70. """Raised when setting the value of a var without first declaring it."""
  71. class StateSchemaMismatchError(ReflexError, TypeError):
  72. """Raised when the serialized schema of a state class does not match the current schema."""
  73. class EnvironmentVarValueError(ReflexError, ValueError):
  74. """Raised when an environment variable is set to an invalid value."""
  75. class DynamicComponentInvalidSignature(ReflexError, TypeError):
  76. """Raised when a dynamic component has an invalid signature."""
  77. class InvalidPropValueError(ReflexError):
  78. """Raised when a prop value is invalid."""
  79. class StateTooLargeError(ReflexError):
  80. """Raised when the state is too large to be serialized."""
  81. class StateSerializationError(ReflexError):
  82. """Raised when the state cannot be serialized."""
  83. class SystemPackageMissingError(ReflexError):
  84. """Raised when a system package is missing."""
  85. def raise_system_package_missing_error(package: str) -> NoReturn:
  86. """Raise a SystemPackageMissingError.
  87. Args:
  88. package: The name of the missing system package.
  89. Raises:
  90. SystemPackageMissingError: The raised exception.
  91. """
  92. from reflex.constants import IS_MACOS
  93. raise SystemPackageMissingError(
  94. f"System package '{package}' is missing."
  95. " Please install it through your system package manager."
  96. + (f" You can do so by running 'brew install {package}'." if IS_MACOS else "")
  97. )
  98. class InvalidLockWarningThresholdError(ReflexError):
  99. """Raised when an invalid lock warning threshold is provided."""
  100. class UnretrievableVarValueError(ReflexError):
  101. """Raised when the value of a var is not retrievable."""