Skip to content

Commit

Permalink
use custom exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ElijahAhianyo committed Oct 2, 2024
1 parent 40bcb72 commit 5bed739
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
EventHandlerShadowsBuiltInStateMethod,
ImmutableStateError,
LockExpiredError,
SetUndefinedStateVarError,
)
from reflex.utils.exec import is_testing_env
from reflex.utils.serializers import SerializedType, serialize, serializer
Expand Down Expand Up @@ -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
Expand All @@ -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."
)
Expand Down
4 changes: 4 additions & 0 deletions reflex/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

0 comments on commit 5bed739

Please sign in to comment.