Skip to content

Commit cd25cb4

Browse files
authored
replace compiled state name with module and name in errors (#5381)
1 parent c994b59 commit cd25cb4

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

reflex/compiler/compiler.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from reflex.utils import console, path_ops
2828
from reflex.utils.exceptions import ReflexError
2929
from reflex.utils.exec import is_prod_mode
30+
from reflex.utils.format import to_title_case
3031
from reflex.utils.imports import ImportVar
3132
from reflex.utils.prerequisites import get_web_dir
3233
from reflex.vars.base import LiteralVar, Var
@@ -672,6 +673,30 @@ def readable_name_from_component(
672673
return None
673674

674675

676+
def _modify_exception(e: Exception) -> None:
677+
"""Modify the exception to make it more readable.
678+
679+
Args:
680+
e: The exception to modify.
681+
"""
682+
if len(e.args) == 1 and isinstance((msg := e.args[0]), str):
683+
while (state_index := msg.find("reflex___")) != -1:
684+
dot_index = msg.find(".", state_index)
685+
if dot_index == -1:
686+
break
687+
state_name = msg[state_index:dot_index]
688+
module_dot_state_name = state_name.replace("___", ".").rsplit("__", 1)[-1]
689+
module_path, _, state_snake_case = module_dot_state_name.rpartition(".")
690+
if not state_snake_case:
691+
break
692+
actual_state_name = to_title_case(state_snake_case)
693+
msg = (
694+
f"{msg[:state_index]}{module_path}.{actual_state_name}{msg[dot_index:]}"
695+
)
696+
697+
e.args = (msg,)
698+
699+
675700
def into_component(component: Component | ComponentCallable) -> Component:
676701
"""Convert a component to a Component.
677702
@@ -696,6 +721,7 @@ def into_component(component: Component | ComponentCallable) -> Component:
696721
component_called = component()
697722
except KeyError as e:
698723
if isinstance(e, ReflexError):
724+
_modify_exception(e)
699725
raise
700726
key = e.args[0] if e.args else None
701727
if key is not None and isinstance(key, Var):
@@ -705,6 +731,7 @@ def into_component(component: Component | ComponentCallable) -> Component:
705731
raise
706732
except TypeError as e:
707733
if isinstance(e, ReflexError):
734+
_modify_exception(e)
708735
raise
709736
message = e.args[0] if e.args else None
710737
if message and isinstance(message, str):
@@ -730,6 +757,9 @@ def into_component(component: Component | ComponentCallable) -> Component:
730757
"Cannot pass a Var to a built-in function. Consider moving the operation to the backend, using existing Var operations, or defining a custom Var operation."
731758
).with_traceback(e.__traceback__) from None
732759
raise
760+
except ReflexError as e:
761+
_modify_exception(e)
762+
raise
733763

734764
if (converted := _into_component_once(component_called)) is not None:
735765
return converted

0 commit comments

Comments
 (0)