Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from reflex.event import (
BACKGROUND_TASK_MARKER,
Event,
EventActionsMixin,
EventHandler,
EventSpec,
fix_events,
Expand Down Expand Up @@ -2359,7 +2360,7 @@ class FrontendEventExceptionState(State):
"""Substate for handling frontend exceptions."""

@event
def handle_frontend_exception(self, stack: str, component_stack: str) -> None:
def handle_frontend_exception(self, stack: str, component_stack: str):
"""Handle frontend exceptions.

If a frontend exception handler is provided, it will be called.
Expand All @@ -2369,11 +2370,31 @@ def handle_frontend_exception(self, stack: str, component_stack: str) -> None:
stack: The stack trace of the exception.
component_stack: The stack trace of the component where the exception occurred.

Returns:
Chained event specs to be executed.

Raises:
TypeError: If the frontend exception handler is not callable.
"""
prerequisites.get_and_validate_app().app.frontend_exception_handler(
Exception(stack)
frontend_exception_handler = (
prerequisites.get_and_validate_app().app.frontend_exception_handler
)

if isinstance(frontend_exception_handler, EventActionsMixin) and callable(
frontend_exception_handler
):
return frontend_exception_handler(stack)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we want to avoid the extra round trip to the client for processing this.

is it possible to do something like

await app.event_namespace.on_event(
    sid=self.router.session.session_id,
    data=fix_events(
        events=[frontend_exception_handler(stack)],
        token=self.router.session.client_token,
        router_data=self.router_data,
    )[0],
)

i guess maybe that would have a dead locking issue because the state is already being processed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe? i couldn't understand exactly how the flow is going and how to make it do that, which is why i couldn't figure out the backend exception and why this one is a draft

if not callable(frontend_exception_handler):
raise TypeError(
f"Frontend exception handler must be callable. Got {type(frontend_exception_handler)}."
)
value = frontend_exception_handler(Exception(stack))
if value is not None:
console.warn(
f"Frontend exception handler returned a value: {value}. This is not supported with plain functions."
" Consider using an Event in a state class instead."
)


class UpdateVarsInternalState(State):
"""Substate for handling internal state var updates."""
Expand Down
Loading