-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add on_exception callbacks #485
Comments
Hi @thedrow, the current way to do this is using from transitions import Machine, Event
class ErrorEvent(Event):
def _process(self, event_data):
try:
return super()._process(event_data)
except Exception:
event_data.args = [event_data.error]
event_data.machine.callbacks(["on_exception"], event_data)
class ErrorMachine(Machine):
event_cls = ErrorEvent
def on_exception(self, error):
print("Help:", error)
def raise_exception(self):
raise Exception("Oh noes!")
m = ErrorMachine(after_state_change='raise_exception')
m.to_initial() Having |
I personally think there's room in the core for this sort of functionality.
Need to think this through, but my initial thought is yes. I'd leave the handling up to the user.
Yes, that seems reasonable. Raise normally unless the callback is registered. |
I need same functionality now(but for AsyncMachine), to not wrap code inside each transition into try except block |
useful for optional callbacks (e.g. #485)
I opted for a simpler less invasive version for |
closing this since there has been no report of issues or improvement suggestions. If you experience issues with |
Can you please showcase an example of using the |
Hello @e0lithic, please open a separate issue or a discussion instead of commenting on a two years old closed issue. from transitions import Machine
class Model:
def handle_error(self):
self.to_failed()
def on_enter_failing(self):
raise RuntimeError("Failing!")
model = Model()
machine = Machine(model=model, states=['initial', 'failing', 'failed'], initial='initial',
on_exception='handle_error')
assert model.is_initial()
model.to_failing()
assert model.is_failed() You should make sure that a failure recovering procedure does not raise exceptions itself. If you end up in an infinite loop, it is very likely that your |
It'd be useful to be able to handle exceptions raised inside callbacks using
on_exception
callbacks.In my case, I want to transition to the
crashed
state whenever an unhandled exception is raised during transitions.The text was updated successfully, but these errors were encountered: