-
Describe the bug The modifications made from the example cited by @aleneum in the example are as follows :
Minimal working example from transitions import Machine
class Model:
def handle_error(self):
self.to_failure()
def on_exit_failing(self):
raise RuntimeError("Failing!")
def __init__(self):
self.machine = Machine(
model=self,
states=[
'initial',
{'name' : 'start', 'on_exit':'on_exit_failing'},
'stop'
], initial='initial',
on_exception='handle_error')
self.machine.add_ordered_transitions(loop=False)
self.machine.add_state("failure")
model = Model()
assert model.is_initial()
model.next_state()
assert model.is_start()
# The next command will cause RecursionError
model.next_state()
assert model.is_failure() Expected behavior |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @e0lithic, it's not the The logic here is:
If your You could a) 'hard set' your model to another state to avoid def handle_error(self):
self.machine.set_state('failure')
self.to_failure() You could b) make the failing operation 'state-sensitive': def handle_error(self):
self.error_state = True
self.to_failure()
def on_exit_failing(self):
if not self.error_state:
raise RuntimeError("Failing!") Or c) attach the critical operation to a transition instead: self.machine = Machine(
model=self,
states=[
'initial',
'start',
'stop'
], initial='initial',
on_exception='handle_error')
self.machine.add_ordered_transitions(loop=False)
self.machine.get_transitions('next_state', 'start', 'stop')[0].add_callback('after', 'failing_cb')
self.machine.add_state("failure") You could also work something out with def handle_error(self, event_data):
tmp = event_data.state.on_exit
event_data.state.on_exit = []
self.to_failure()
event_data.state.on_exit = tmp note that you have to add 'event_data' as a parameter to ALL callbacks if you set Personally, I'd probably go for c), a) or d) in that order. |
Beta Was this translation helpful? Give feedback.
Hello @e0lithic,
it's not the
on_exception
callback that causes recursion errors here but the repetitive attempt to exitfailure
.The logic here is:
If your
on_exit
callback may fail you need another approach.You could a) 'hard set' your model to another state to avoid
on_exit_failing
:You could b) make the failing operation 'state-sensitive':