|
| 1 | + |
| 2 | +(error-handling)= |
| 3 | +# Error handling |
| 4 | + |
| 5 | +When callbacks raise exceptions during a transition, the state machine needs |
| 6 | +a strategy. This library supports two models: **exception propagation** (the |
| 7 | +traditional approach) and **error events** (the SCXML-compliant approach). |
| 8 | + |
| 9 | +## Two models |
| 10 | + |
| 11 | +| Approach | `error_on_execution` | Behavior | |
| 12 | +|-----------------------|----------------------|-------------------------------------------------------| |
| 13 | +| Exception propagation | `False` | Exceptions bubble up to the caller like normal Python. | |
| 14 | +| Error events | `True` | Exceptions are caught and dispatched as `error.execution` internal events. | |
| 15 | + |
| 16 | +`StateChart` uses `error_on_execution = True` by default — the SCXML-compliant |
| 17 | +behavior. You can switch to exception propagation by overriding the attribute: |
| 18 | + |
| 19 | +```python |
| 20 | +from statemachine import StateChart |
| 21 | + |
| 22 | +class MyChart(StateChart): |
| 23 | + error_on_execution = False # exceptions propagate to the caller |
| 24 | +``` |
| 25 | + |
| 26 | +### When to use which |
| 27 | + |
| 28 | +**Exception propagation** (`error_on_execution = False`) is simpler and |
| 29 | +familiar to most Python developers. It works well for flat state machines |
| 30 | +where errors should stop execution immediately and be handled by the caller |
| 31 | +with `try/except`. |
| 32 | + |
| 33 | +**Error events** (`error_on_execution = True`) are the SCXML standard and |
| 34 | +the recommended approach for statecharts. They allow the machine to handle |
| 35 | +errors as part of its own logic — transitioning to error states, retrying, |
| 36 | +or recovering — without leaking implementation details to the caller. This |
| 37 | +is especially powerful in hierarchical machines where different states may |
| 38 | +handle errors differently. |
| 39 | + |
| 40 | + |
| 41 | +(error-execution)= |
| 42 | +## Error events with `error.execution` |
| 43 | + |
| 44 | +When `error_on_execution` is `True`, runtime exceptions during transitions |
| 45 | +are caught by the engine and result in an internal `error.execution` event |
| 46 | +being placed on the queue. This follows the |
| 47 | +[SCXML error handling specification](https://www.w3.org/TR/scxml/#errorsAndEvents). |
| 48 | + |
| 49 | +You can define transitions for this event to gracefully handle errors within |
| 50 | +the state machine itself. |
| 51 | + |
| 52 | +### The `error_` naming convention |
| 53 | + |
| 54 | +Since Python identifiers cannot contain dots, the library provides a naming |
| 55 | +convention: any event attribute starting with `error_` automatically matches |
| 56 | +both the underscore form and the dot-notation form. For example, |
| 57 | +`error_execution` matches both `"error_execution"` and `"error.execution"`. |
| 58 | + |
| 59 | +```py |
| 60 | +>>> from statemachine import State, StateChart |
| 61 | + |
| 62 | +>>> class MyChart(StateChart): |
| 63 | +... s1 = State("s1", initial=True) |
| 64 | +... error_state = State("error_state", final=True) |
| 65 | +... |
| 66 | +... go = s1.to(s1, on="bad_action") |
| 67 | +... error_execution = s1.to(error_state) |
| 68 | +... |
| 69 | +... def bad_action(self): |
| 70 | +... raise RuntimeError("something went wrong") |
| 71 | + |
| 72 | +>>> sm = MyChart() |
| 73 | +>>> sm.send("go") |
| 74 | +>>> sm.configuration == {sm.error_state} |
| 75 | +True |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +This is equivalent to the more verbose explicit form: |
| 80 | + |
| 81 | +```python |
| 82 | +error_execution = Event(s1.to(error_state), id="error.execution") |
| 83 | +``` |
| 84 | + |
| 85 | +The convention works with both bare transitions and `Event` objects without |
| 86 | +an explicit `id`: |
| 87 | + |
| 88 | +```py |
| 89 | +>>> from statemachine import Event, State, StateChart |
| 90 | + |
| 91 | +>>> class ChartWithEvent(StateChart): |
| 92 | +... s1 = State("s1", initial=True) |
| 93 | +... error_state = State("error_state", final=True) |
| 94 | +... |
| 95 | +... go = s1.to(s1, on="bad_action") |
| 96 | +... error_execution = Event(s1.to(error_state)) |
| 97 | +... |
| 98 | +... def bad_action(self): |
| 99 | +... raise RuntimeError("something went wrong") |
| 100 | + |
| 101 | +>>> sm = ChartWithEvent() |
| 102 | +>>> sm.send("go") |
| 103 | +>>> sm.configuration == {sm.error_state} |
| 104 | +True |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +```{note} |
| 109 | +If you provide an explicit `id=` parameter, it takes precedence and the naming |
| 110 | +convention is not applied. |
| 111 | +``` |
| 112 | + |
| 113 | +### Accessing error data |
| 114 | + |
| 115 | +The error object is passed as `error` in the keyword arguments to callbacks |
| 116 | +on the `error.execution` transition: |
| 117 | + |
| 118 | +```py |
| 119 | +>>> from statemachine import State, StateChart |
| 120 | + |
| 121 | +>>> class ErrorDataChart(StateChart): |
| 122 | +... s1 = State("s1", initial=True) |
| 123 | +... error_state = State("error_state", final=True) |
| 124 | +... |
| 125 | +... go = s1.to(s1, on="bad_action") |
| 126 | +... error_execution = s1.to(error_state, on="handle_error") |
| 127 | +... |
| 128 | +... def bad_action(self): |
| 129 | +... raise RuntimeError("specific error") |
| 130 | +... |
| 131 | +... def handle_error(self, error=None, **kwargs): |
| 132 | +... self.last_error = error |
| 133 | + |
| 134 | +>>> sm = ErrorDataChart() |
| 135 | +>>> sm.send("go") |
| 136 | +>>> str(sm.last_error) |
| 137 | +'specific error' |
| 138 | + |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | +### Error-in-error-handler behavior |
| 143 | + |
| 144 | +If an error occurs while processing the `error.execution` event itself, the |
| 145 | +engine ignores the second error (logging a warning) to prevent infinite loops. |
| 146 | +The state machine remains in the configuration it was in before the failed |
| 147 | +error handler. |
| 148 | + |
| 149 | + |
| 150 | +## Block-level error catching |
| 151 | + |
| 152 | +`StateChart` catches errors at the **block level**, not the microstep level. |
| 153 | +Each phase of the microstep — `on_exit`, transition `on` content, `on_enter` |
| 154 | +— is an independent block. An error in one block: |
| 155 | + |
| 156 | +- **Stops remaining actions in that block** (per SCXML spec, execution MUST |
| 157 | + NOT continue within the same block after an error). |
| 158 | +- **Does not affect other blocks** — subsequent phases of the microstep still |
| 159 | + execute. In particular, `after` callbacks always run regardless of errors |
| 160 | + in earlier blocks. |
| 161 | + |
| 162 | +This means that even if a transition's `on` action raises an exception, the |
| 163 | +transition completes: target states are entered and `after_<event>()` callbacks |
| 164 | +still run. The error is caught and queued as an `error.execution` internal |
| 165 | +event, which can be handled by a separate transition. |
| 166 | + |
| 167 | +```{note} |
| 168 | +During `error.execution` processing, errors in transition `on` content are |
| 169 | +**not** caught at block level — they propagate to the microstep, where they |
| 170 | +are silently ignored. This prevents infinite loops when an error handler's own |
| 171 | +action raises (e.g., a self-transition `error_execution = s1.to(s1, on="handler")` |
| 172 | +where `handler` raises). Entry/exit blocks always use block-level error |
| 173 | +catching regardless of the current event. |
| 174 | +``` |
| 175 | + |
| 176 | + |
| 177 | +(error-handling-cleanup-finalize)= |
| 178 | +## Cleanup / finalize pattern |
| 179 | + |
| 180 | +A common need is to run cleanup code after a transition **regardless of |
| 181 | +success or failure** — for example, releasing a lock or closing a resource. |
| 182 | + |
| 183 | +Because `StateChart` catches errors at the block level (see above), |
| 184 | +`after_<event>()` callbacks still run even when an action raises an exception. |
| 185 | +This makes `after_<event>()` a natural **finalize** hook — no need to |
| 186 | +duplicate cleanup logic in an error handler. |
| 187 | + |
| 188 | +For error-specific handling (logging, recovery), define an `error.execution` |
| 189 | +transition and use {func}`raise_() <StateChart.raise_>` to auto-recover |
| 190 | +within the same macrostep. |
| 191 | + |
| 192 | +See the full working example in {ref}`sphx_glr_auto_examples_statechart_cleanup_machine.py`. |
| 193 | + |
| 194 | + |
| 195 | +```{seealso} |
| 196 | +See {ref}`statecharts` for the full comparison of `StateChart` behavioral |
| 197 | +attributes and how to customize them. |
| 198 | +``` |
0 commit comments