Skip to content

Commit ff9323d

Browse files
author
Maxim Vlah
committed
feat: nullable exception handlers
1 parent e4180b8 commit ff9323d

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

reflex/app.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,14 @@ class App(MiddlewareMixin, LifespanMixin):
422422
_background_tasks: set[asyncio.Task] = dataclasses.field(default_factory=set)
423423

424424
# Frontend Error Handler Function
425-
frontend_exception_handler: Callable[[Exception], None] = (
425+
frontend_exception_handler: Callable[[Exception], None] | None = (
426426
default_frontend_exception_handler
427427
)
428428

429429
# Backend Error Handler Function
430-
backend_exception_handler: Callable[
431-
[Exception], EventSpec | list[EventSpec] | None
432-
] = default_backend_exception_handler
430+
backend_exception_handler: (
431+
Callable[[Exception], EventSpec | list[EventSpec] | None] | None
432+
) = default_backend_exception_handler
433433

434434
# Put the toast provider in the app wrap.
435435
toaster: Component | None = dataclasses.field(default_factory=toast.provider)
@@ -1620,6 +1620,10 @@ def _validate_exception_handlers(self):
16201620
],
16211621
strict=True,
16221622
):
1623+
if handler_fn is None:
1624+
# If the handler is None, skip validation.
1625+
continue
1626+
16231627
if hasattr(handler_fn, "__name__"):
16241628
_fn_name = handler_fn.__name__
16251629
else:
@@ -1773,7 +1777,8 @@ async def process(
17731777
except Exception as ex:
17741778
telemetry.send_error(ex, context="backend")
17751779

1776-
app.backend_exception_handler(ex)
1780+
if app.backend_exception_handler is not None:
1781+
app.backend_exception_handler(ex)
17771782
raise
17781783

17791784

reflex/state.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,8 +1880,12 @@ async def _process_event(
18801880
except Exception as ex:
18811881
telemetry.send_error(ex, context="backend")
18821882

1883+
app = prerequisites.get_and_validate_app().app
1884+
18831885
event_specs = (
1884-
prerequisites.get_and_validate_app().app.backend_exception_handler(ex)
1886+
app.backend_exception_handler(ex)
1887+
if app.backend_exception_handler
1888+
else None
18851889
)
18861890

18871891
yield await state._as_state_update(
@@ -2433,9 +2437,10 @@ def handle_frontend_exception(self, info: str, component_stack: str) -> None:
24332437
component_stack: The stack trace of the component where the exception occurred.
24342438
24352439
"""
2436-
prerequisites.get_and_validate_app().app.frontend_exception_handler(
2437-
Exception(info)
2438-
)
2440+
app = prerequisites.get_and_validate_app().app
2441+
2442+
if app.frontend_exception_handler is not None:
2443+
app.frontend_exception_handler(Exception(info))
24392444

24402445

24412446
class UpdateVarsInternalState(State):

0 commit comments

Comments
 (0)