I am using the transitions FSM library. Imagine having an application FSM using the following code:
from transitions import Machine
import os
class Application(object):
states = ["idle", "data_loaded"]
def __init__(self):
self.data = None
machine = Machine(model=self, states=Application.states, initial="idle")
machine.add_transition("filename_dropped",
source="idle",
dest="data_loaded",
before="load_data",
conditions="is_valid_filename")
self.machine = machine
def drop_filename(self, filename):
try:
self.filename_dropped(filename)
except IOError as exc:
print "Oops: %s" % str(exc)
def load_data(self, filename):
with open(filename) as file:
self.data = file.read()
def is_valid_filename(self, filename):
return os.path.isfile(filename)
It can throw an IOError
within load_data. My question is whether it is safe to raise exceptions (either implicitly like in this example or explicitly) from within before
callbacks? In case of an IOError
the transition is not taking place, the state of this example remains idle
and any after
callbacks are not being invoked. However, I wonder whether the internal state of the machine instance might get corrupted.
Additional question: Are there any better ways to signal errors with concrete information to the application? In this very example I could use the condition to load the file, but this seems ugly and I would need some additional attribute to keep track of the error etc.
Thanks for any help or advice.