Quellcode durchsuchen

improve error message for failed compile_state (#4702)

Khaleel Al-Adhami vor 3 Monaten
Ursprung
Commit
2a922214a2
2 geänderte Dateien mit 9 neuen und 1 gelöschten Zeilen
  1. 7 1
      reflex/compiler/utils.py
  2. 2 0
      reflex/constants/base.py

+ 7 - 1
reflex/compiler/utils.py

@@ -2,6 +2,8 @@
 
 
 from __future__ import annotations
 from __future__ import annotations
 
 
+import traceback
+from datetime import datetime
 from pathlib import Path
 from pathlib import Path
 from typing import Any, Callable, Dict, Optional, Type, Union
 from typing import Any, Callable, Dict, Optional, Type, Union
 from urllib.parse import urlparse
 from urllib.parse import urlparse
@@ -165,8 +167,12 @@ def compile_state(state: Type[BaseState]) -> dict:
     try:
     try:
         initial_state = state(_reflex_internal_init=True).dict(initial=True)
         initial_state = state(_reflex_internal_init=True).dict(initial=True)
     except Exception as e:
     except Exception as e:
+        timestamp = datetime.now().strftime("%Y-%m-%d__%H-%M-%S")
+        constants.Reflex.LOGS_DIR.mkdir(parents=True, exist_ok=True)
+        log_path = constants.Reflex.LOGS_DIR / f"state_compile_error_{timestamp}.log"
+        traceback.TracebackException.from_exception(e).print(file=log_path.open("w+"))
         console.warn(
         console.warn(
-            f"Failed to compile initial state with computed vars, excluding them: {e}"
+            f"Failed to compile initial state with computed vars. Error log saved to {log_path}"
         )
         )
         initial_state = state(_reflex_internal_init=True).dict(
         initial_state = state(_reflex_internal_init=True).dict(
             initial=True, include_computed=False
             initial=True, include_computed=False

+ 2 - 0
reflex/constants/base.py

@@ -75,6 +75,8 @@ class Reflex(SimpleNamespace):
     # If user sets REFLEX_DIR envroment variable use that instead.
     # If user sets REFLEX_DIR envroment variable use that instead.
     DIR = PlatformDirs(MODULE_NAME, False).user_data_path
     DIR = PlatformDirs(MODULE_NAME, False).user_data_path
 
 
+    LOGS_DIR = DIR / "logs"
+
     # The root directory of the reflex library.
     # The root directory of the reflex library.
     ROOT_DIR = Path(__file__).parents[2]
     ROOT_DIR = Path(__file__).parents[2]