From 523cd2eb50caec449bc9e0d3b76c0382add09838 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Tue, 15 Oct 2024 19:30:30 -0700 Subject: [PATCH] add type hinting to error boundary --- reflex/components/base/error_boundary.py | 28 +++++++++++++++++++---- reflex/components/base/error_boundary.pyi | 11 ++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/reflex/components/base/error_boundary.py b/reflex/components/base/error_boundary.py index 66a9c43c8..b8b4ef3a6 100644 --- a/reflex/components/base/error_boundary.py +++ b/reflex/components/base/error_boundary.py @@ -2,7 +2,7 @@ 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 @@ -14,6 +14,22 @@ 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]]: + """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 (info.componentStack,) + + +LOG_FRONTEND_ERROR = Var("logFrontendError").to(FunctionVar, EventChain) + + class ErrorBoundary(Component): """A React Error Boundary component that catches unhandled frontend exceptions.""" @@ -21,9 +37,7 @@ 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] = LOG_FRONTEND_ERROR # type: ignore # Rendered instead of the children when an error is caught. Fallback_component: Var[Component] = Var(_js_expr="Fallback")._replace( @@ -44,7 +58,11 @@ class ErrorBoundary(Component): Returns: The hooks to add. """ - return [Hooks.EVENTS, Hooks.FRONTEND_ERRORS] + return ( + [Hooks.EVENTS, Hooks.FRONTEND_ERRORS] + if "on_error" not in self.event_triggers + else [] + ) def add_custom_code(self) -> List[str]: """Add custom Javascript code into the page that contains this component. diff --git a/reflex/components/base/error_boundary.pyi b/reflex/components/base/error_boundary.pyi index 65109b0fe..80b6880ba 100644 --- a/reflex/components/base/error_boundary.pyi +++ b/reflex/components/base/error_boundary.pyi @@ -3,13 +3,18 @@ # ------------------- DO NOT EDIT ---------------------- # This file was generated by `reflex/utils/pyi_generator.py`! # ------------------------------------------------------ -from typing import Any, Dict, List, Optional, Union, overload +from typing import Any, Dict, List, Optional, Tuple, Union, overload from reflex.components.component import Component -from reflex.event import EventType +from reflex.event import EventChain, EventType from reflex.style import Style from reflex.utils.imports import ImportVar 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]]: ... + +LOG_FRONTEND_ERROR = Var("logFrontendError").to(FunctionVar, EventChain) class ErrorBoundary(Component): def add_imports(self) -> dict[str, list[ImportVar]]: ... @@ -31,7 +36,7 @@ class ErrorBoundary(Component): on_click: Optional[EventType[[]]] = None, on_context_menu: Optional[EventType[[]]] = None, on_double_click: Optional[EventType[[]]] = None, - on_error: Optional[EventType[[]]] = None, + on_error: Optional[EventType[str]] = None, on_focus: Optional[EventType[[]]] = None, on_mount: Optional[EventType[[]]] = None, on_mouse_down: Optional[EventType[[]]] = None,