From cd91136de67bc62faf56a99f17ff60f3ef9be1e2 Mon Sep 17 00:00:00 2001 From: Elijah Date: Wed, 2 Oct 2024 17:27:41 +0000 Subject: [PATCH] use custom exception --- reflex/state.py | 5 +++-- reflex/utils/exceptions.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/reflex/state.py b/reflex/state.py index 95a767fea..5e9783273 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -74,6 +74,7 @@ from reflex.utils.exceptions import ( EventHandlerShadowsBuiltInStateMethod, ImmutableStateError, LockExpiredError, + SetUndefinedStateVarError, ) from reflex.utils.exec import is_testing_env from reflex.utils.serializers import serializer @@ -1262,7 +1263,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): value: The value of the attribute. Raises: - AttributeError: If a value of a var is set without first defining it. + SetUndefinedStateVar: If a value of a var is set without first defining it. """ if isinstance(value, MutableProxy): # unwrap proxy objects when assigning back to the state @@ -1287,7 +1288,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): and name not in self.vars and name not in self.get_skip_vars() ): - raise AttributeError( + raise SetUndefinedStateVarError( f"The state variable '{name}' has not been defined in '{type(self).__name__}'. " f"All state variables must be declared before they can be set." ) diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index 7c3532861..9c79a387a 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -115,3 +115,7 @@ class PrimitiveUnserializableToJSON(ReflexError, ValueError): class InvalidLifespanTaskType(ReflexError, TypeError): """Raised when an invalid task type is registered as a lifespan task.""" + + +class SetUndefinedStateVarError(ReflexError, AttributeError): + """Raised when setting the value of a var without first declaring it."""