added name of Var to error message (#2827)

This commit is contained in:
Emmanuel Onwuegbusi 2024-03-13 23:39:49 +01:00 committed by GitHub
parent 5701a72c8f
commit 2a8d9d1ef4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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: