diff --git a/reflex/state.py b/reflex/state.py index 55f29cf45..899ce528d 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -97,6 +97,7 @@ from reflex.utils.exceptions import ( ReflexRuntimeError, SetUndefinedStateVarError, StateSchemaMismatchError, + StateSerializationError, StateTooLargeError, ) from reflex.utils.exec import is_testing_env @@ -2177,8 +2178,12 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): Returns: The serialized state. + + Raises: + StateSerializationError: If the state cannot be serialized. """ payload = b"" + error = "" try: payload = pickle.dumps((self._to_schema(), self)) except HANDLED_PICKLE_ERRORS as og_pickle_error: @@ -2198,8 +2203,13 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): except HANDLED_PICKLE_ERRORS as ex: error += f"Dill was also unable to pickle the state: {ex}" console.warn(error) + if environment.REFLEX_PERF_MODE.get() != PerformanceMode.OFF: self._check_state_size(len(payload)) + + if not payload: + raise StateSerializationError(error) + return payload @classmethod diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index 714dc912c..a89c4d4aa 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -155,6 +155,10 @@ class StateTooLargeError(ReflexError): """Raised when the state is too large to be serialized.""" +class StateSerializationError(ReflexError): + """Raised when the state cannot be serialized.""" + + class SystemPackageMissingError(ReflexError): """Raised when a system package is missing."""