Sfoglia il codice sorgente

added name of Var to error message (#2827)

Emmanuel Onwuegbusi 1 anno fa
parent
commit
2a8d9d1ef4
1 ha cambiato i file con 12 aggiunte e 4 eliminazioni
  1. 12 4
      reflex/utils/format.py

+ 12 - 4
reflex/utils/format.py

@@ -6,7 +6,7 @@ import inspect
 import json
 import os
 import re
-from typing import TYPE_CHECKING, Any, List, Union
+from typing import TYPE_CHECKING, Any, List, Optional, Union
 
 from reflex import constants
 from reflex.utils import exceptions, serializers, types
@@ -603,11 +603,12 @@ def format_query_params(router_data: dict[str, Any]) -> dict[str, str]:
     return {k.replace("-", "_"): v for k, v in params.items()}
 
 
-def format_state(value: Any) -> Any:
+def format_state(value: Any, key: Optional[str] = None) -> Any:
     """Recursively format values in the given state.
 
     Args:
         value: The state to format.
+        key: The key associated with the value (optional).
 
     Returns:
         The formatted state.
@@ -617,7 +618,7 @@ def format_state(value: Any) -> Any:
     """
     # Handle dicts.
     if isinstance(value, dict):
-        return {k: format_state(v) for k, v in value.items()}
+        return {k: format_state(v, k) for k, v in value.items()}
 
     # Handle lists, sets, typles.
     if isinstance(value, types.StateIterBases):
@@ -632,7 +633,14 @@ def format_state(value: Any) -> Any:
     if serialized is not None:
         return serialized
 
-    raise TypeError(f"No JSON serializer found for var {value} of type {type(value)}.")
+    if key is None:
+        raise TypeError(
+            f"No JSON serializer found for var {value} of type {type(value)}."
+        )
+    else:
+        raise TypeError(
+            f"No JSON serializer found for State Var '{key}' of value {value} of type {type(value)}."
+        )
 
 
 def format_state_name(state_name: str) -> str: