use custom exception

This commit is contained in:
Elijah 2024-10-02 17:27:41 +00:00
parent 28f210c519
commit cd91136de6
2 changed files with 7 additions and 2 deletions

View File

@ -74,6 +74,7 @@ from reflex.utils.exceptions import (
EventHandlerShadowsBuiltInStateMethod, EventHandlerShadowsBuiltInStateMethod,
ImmutableStateError, ImmutableStateError,
LockExpiredError, LockExpiredError,
SetUndefinedStateVarError,
) )
from reflex.utils.exec import is_testing_env from reflex.utils.exec import is_testing_env
from reflex.utils.serializers import serializer from reflex.utils.serializers import serializer
@ -1262,7 +1263,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
value: The value of the attribute. value: The value of the attribute.
Raises: 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): if isinstance(value, MutableProxy):
# unwrap proxy objects when assigning back to the state # 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.vars
and name not in self.get_skip_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"The state variable '{name}' has not been defined in '{type(self).__name__}'. "
f"All state variables must be declared before they can be set." f"All state variables must be declared before they can be set."
) )

View File

@ -115,3 +115,7 @@ class PrimitiveUnserializableToJSON(ReflexError, ValueError):
class InvalidLifespanTaskType(ReflexError, TypeError): class InvalidLifespanTaskType(ReflexError, TypeError):
"""Raised when an invalid task type is registered as a lifespan task.""" """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."""