diff --git a/reflex/state.py b/reflex/state.py index 0abca47038..3b35a2d893 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -71,6 +71,7 @@ EventHandlerShadowsBuiltInStateMethod, ImmutableStateError, LockExpiredError, + SetUndefinedStateVarError, ) from reflex.utils.exec import is_testing_env from reflex.utils.serializers import SerializedType, serialize, serializer @@ -1218,7 +1219,7 @@ def __setattr__(self, name: str, value: Any): 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 @@ -1243,7 +1244,7 @@ def __setattr__(self, name: str, value: Any): 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 95d68c3b8b..d77897bcde 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -111,3 +111,7 @@ class GeneratedCodeHasNoFunctionDefs(ReflexError): class PrimitiveUnserializableToJSON(ReflexError, ValueError): """Raised when a primitive type is unserializable to JSON. Usually with NaN and Infinity.""" + + +class SetUndefinedStateVarError(ReflexError, AttributeError): + """Raised when setting the value of a var without first declaring it."""