Skip to content

Commit

Permalink
raise StateSerializationError if the state cannot be serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher committed Nov 28, 2024
1 parent 39cdce6 commit 4b7a1de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
ReflexRuntimeError,
SetUndefinedStateVarError,
StateSchemaMismatchError,
StateSerializationError,
StateTooLargeError,
)
from reflex.utils.exec import is_testing_env
Expand Down Expand Up @@ -2177,8 +2178,12 @@ def _serialize(self) -> bytes:
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:
Expand All @@ -2198,8 +2203,13 @@ def _serialize(self) -> bytes:
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) from None

return payload

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions reflex/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down

0 comments on commit 4b7a1de

Please sign in to comment.