feat(core): exception transitions via on_error capture and wildcard routing (#30)
Summary
Implements #30 "Add exception transitions". Currently an exception in an action breaks control flow and stops the program. This adds the ability to suppress an exception, capture it into state, and route to an error-handling action via a wildcard transition.
Scope chosen: capture + wildcard (combines ideas #3 and #4 from the issue thread).
from burr.core import action, capture_as, expr, ApplicationBuilder, State
@action(reads=[], writes=["output"], on_error=capture_as("error"))
def flaky(state: State) -> tuple[dict, State]:
result = {"output": call_some_api(...)}
return result, state.update(**result)
@action(reads=["error"], writes=[])
def handler(state: State) -> tuple[dict, State]:
... # inspect / reset state["error"], then route onward
app = (
ApplicationBuilder()
.with_actions(flaky=flaky, handler=handler)
.with_state(error=None) # seed: expr() raises on a missing key
.with_transitions(("*", "handler", expr("error is not None")))
.with_entrypoint("flaky")
.build()
)
What's included
capture_as(field, include_traceback=True)— anon_errorhandler that writes a JSON-serializable{type, message, traceback}record into a state field. Exported fromburr.core.@action(..., on_error=handler)— per-action handler, any callable(State, Exception) -> State.ApplicationBuilder.with_error_handling(handler)— builder-level global handler for actions without their ownon_error. Per-action takes precedence.- Wildcard
("*", target, condition)transitions — route from any action.
The captured field bypasses reducer write-validation (need not be in writes). A failing handler never masks the original exception (re-raised with the handler error as cause). Captured records are JSON-serializable, so persistence/tracking are unaffected.
Behavior changes to call out for review
These go beyond the headline feature, kept as separate commits:
- 4-tier transition resolution (
fix(core)commit). The fix for the precedence bug that kept #30 open: an action's owndefaulttransition shadowed a guarded wildcard route, so the route never fired.get_next_nodenow resolves: source non-default, wildcard non-default, source default, wildcard default. Semantic change: in default-FIRST graphs a previously-dead non-default transition becomes live. Default-LAST graphs (the common case) and graphs without wildcards are unchanged, covered by a regression test. _asteprestructure. A SYNC action driven through the async path now reports the realresult/stateto the asyncpost_run_stephook instead of stale values (it previously delegated to_stepand discarded them). Locked by a regression test.
Out of scope (follow-up)
Streaming error handling (@streaming_action on_error during incremental stream_result/astream_result consumption) is intentionally not included, to avoid shipping a silently inert API. Tracked as a follow-up on #30.
Testing
pytest tests/core/green (363 tests, +17 new).- Full non-integration suite green (416 passed, 1 skipped).
- Verified wildcard graphs survive
visualize()andApplicationModelserialization (the tracking UI path). flake8clean,black/isortapplied.
Process
The design never converged on the list (the issue thread has 4 competing shapes). This PR is a concrete proposal. I will start a [DISCUSS] thread on [email protected] referencing this branch before merge, per podling process. Feedback on the precedence semantics in particular is welcome.
Closes #30.