Error handling¶
Catch errors raised while rendering with Errored, show a fallback, and recover with a reset callback or automatically when a route changes.
from wybthon import Errored, component, create_signal, html, render
from wybthon.router import url
@component
def RiskyPanel(should_fail: bool = False):
# A hole that raises routes the error to the nearest Errored boundary.
def body():
if should_fail:
raise RuntimeError("the panel exploded")
return "Everything is fine."
return html(t"<p>{body()}</p>")
def fallback(err, reset):
return html(t"""
<div style="color: crimson">
<p>Caught: {err()}</p>
<button onclick={reset}>Retry</button>
</div>
""")
def log_error(err):
print("logged:", err)
@component
def App():
fail, set_fail = create_signal(False)
def toggle():
set_fail(lambda v: not v)
return html(t"""
<div>
<button onclick={toggle}>Toggle failure</button>
{Errored(RiskyPanel(should_fail=fail()), fallback=fallback, on_error=log_error, reset_on=url)}
</div>
""")
render(App(), "#app")
How it works¶
Erroredinstalls an error handler on its owner scope. Errors raised in a descendant hole, component body, effect, or async memo route to the nearest boundary; sibling trees are untouched.- Its children can be a node, as here.
RiskyPanel(...)is a component call inErrored's content, so the compiler makes it a zero-argument function and the panel is built only when the boundary builds it; there's no need to wrap it in a lambda. Because the call sits in a template, the compiler still passesshould_failas a getter, so the panel reads the current value offail()each timebody()runs. body()is a plain function call in the template, so the whole call is one reactive hole: it reruns when theshould_failprop changes, and when it raises, the error goes to the boundary.fallbackmay be a node, a string, or a callable. A callable receives(err, reset)(or just(err)).erris an accessor for the caught exception, as in Solid 2.0, so{err()}in the fallback's template shows the message. Callingreset()clears the error and re-renders the children; it takes no arguments, so it works directly as a click handler.on_erroris a plain callback for logging or reporting. It receives the exception itself.reset_onis an accessor whose change clears the error automatically. Passing theurlaccessor fromwybthon.routerresets the boundary on every navigation, which is the usual behavior for a page-level boundary.- Boundaries also heal on their own: the boundary remembers which reactive inputs the failing computation read, so toggling the failure off re-renders
RiskyPanelwithout a click on "Retry."
Errors in async data¶
An async memo that rejects raises into the boundary as well. Nest Errored outside Loading so the fallback replaces the pending UI:
from wybthon import Errored, Loading, component, create_memo, html
async def load_profile():
response = await fetch_json("/api/profile")
if response is None:
raise LookupError("profile not found")
return response
def profile_failed(err, reset):
return html(t"""
<div>
<p>{err()}</p>
<button onclick={reset}>Try again</button>
</div>
""")
@component
def Profile():
profile = create_memo(load_profile)
return Errored(
Loading(html(t"<p>{profile()['name']}</p>"), fallback=html(t"<p>Loading...</p>")),
fallback=profile_failed,
)
The memo keeps its error until it runs again, so to retry the request itself, call refresh on it before reset(), as the Async fetch example does.
Errors in effects¶
create_effect accepts an error= handler that receives exceptions from its compute stage instead of routing them to the boundary:
from wybthon import create_effect
create_effect(
lambda: parse(raw_input()),
lambda parsed: show(parsed),
error=lambda exc: print("parse failed:", exc),
)
Event handlers run outside rendering, so an exception in an onclick handler is logged to the console and doesn't involve any boundary; the UI stays intact.
Next steps¶
- Read the Error Boundaries concept page.
- See the
error_boundaryAPI reference. - Combine with Loading for async failure handling.