[statemachine] state run should not raise error

Crash when run method raises error. This is better
than the alternative, which is to silently swallow
the error.
This commit is contained in:
Mark Spanbroek 2023-02-13 17:18:38 +01:00
parent 50130c33f5
commit 21527bb70f
No known key found for this signature in database
GPG Key ID: FBE3E9548D427C00
2 changed files with 7 additions and 2 deletions

View File

@ -25,8 +25,11 @@ template makeStateMachine*(MachineType, StateType) =
discard
proc run(machine: MachineType, state: StateType) {.async.} =
if next =? await state.run():
machine.schedule(Event.transition(state, next))
try:
if next =? await state.run():
machine.schedule(Event.transition(state, next))
except CancelledError:
discard
proc scheduler(machine: MachineType) {.async.} =
try:
@ -37,6 +40,7 @@ template makeStateMachine*(MachineType, StateType) =
await machine.running.cancelAndWait()
machine.state = next
machine.running = machine.run(machine.state)
asyncSpawn machine.running
except CancelledError:
discard

View File

@ -27,6 +27,7 @@ method run(state: State2): Future[?State] {.async.} =
await sleepAsync(1.hours)
except CancelledError:
inc cancellations[1]
raise
method onMoveToNextStateEvent(state: State2): ?State =
some State(State3.new())