|
@@ -2,16 +2,30 @@
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
-from typing import List
|
|
|
+from typing import Dict, List, Tuple
|
|
|
|
|
|
from reflex.compiler.compiler import _compile_component
|
|
|
from reflex.components.component import Component
|
|
|
from reflex.components.el import div, p
|
|
|
-from reflex.constants import Hooks, Imports
|
|
|
-from reflex.event import EventChain, EventHandler
|
|
|
-from reflex.utils.imports import ImportVar
|
|
|
+from reflex.event import EventHandler
|
|
|
+from reflex.state import FrontendEventExceptionState
|
|
|
from reflex.vars.base import Var
|
|
|
-from reflex.vars.function import FunctionVar
|
|
|
+
|
|
|
+
|
|
|
+def on_error_spec(error: Var, info: Var[Dict[str, str]]) -> Tuple[Var[str], Var[str]]:
|
|
|
+ """The spec for the on_error event handler.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ error: The error message.
|
|
|
+ info: Additional information about the error.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ The arguments for the event handler.
|
|
|
+ """
|
|
|
+ return (
|
|
|
+ error.stack,
|
|
|
+ info.componentStack,
|
|
|
+ )
|
|
|
|
|
|
|
|
|
class ErrorBoundary(Component):
|
|
@@ -21,31 +35,13 @@ class ErrorBoundary(Component):
|
|
|
tag = "ErrorBoundary"
|
|
|
|
|
|
# Fired when the boundary catches an error.
|
|
|
- on_error: EventHandler[lambda error, info: [error, info]] = Var( # type: ignore
|
|
|
- "logFrontendError"
|
|
|
- ).to(FunctionVar, EventChain)
|
|
|
+ on_error: EventHandler[on_error_spec]
|
|
|
|
|
|
# Rendered instead of the children when an error is caught.
|
|
|
Fallback_component: Var[Component] = Var(_js_expr="Fallback")._replace(
|
|
|
_var_type=Component
|
|
|
)
|
|
|
|
|
|
- def add_imports(self) -> dict[str, list[ImportVar]]:
|
|
|
- """Add imports for the component.
|
|
|
-
|
|
|
- Returns:
|
|
|
- The imports to add.
|
|
|
- """
|
|
|
- return Imports.EVENTS
|
|
|
-
|
|
|
- def add_hooks(self) -> List[str | Var]:
|
|
|
- """Add hooks for the component.
|
|
|
-
|
|
|
- Returns:
|
|
|
- The hooks to add.
|
|
|
- """
|
|
|
- return [Hooks.EVENTS, Hooks.FRONTEND_ERRORS]
|
|
|
-
|
|
|
def add_custom_code(self) -> List[str]:
|
|
|
"""Add custom Javascript code into the page that contains this component.
|
|
|
|
|
@@ -75,5 +71,20 @@ class ErrorBoundary(Component):
|
|
|
"""
|
|
|
]
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def create(cls, *children, **props):
|
|
|
+ """Create an ErrorBoundary component.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ *children: The children of the component.
|
|
|
+ **props: The props of the component.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ The ErrorBoundary component.
|
|
|
+ """
|
|
|
+ if "on_error" not in props:
|
|
|
+ props["on_error"] = FrontendEventExceptionState.handle_frontend_exception
|
|
|
+ return super().create(*children, **props)
|
|
|
+
|
|
|
|
|
|
error_boundary = ErrorBoundary.create
|