views:

573

answers:

2

When a method being called in the success or enter phases of a state transition throw errors, what is the best way to catch this and ensure that the state reverts back to the previous state.

I'm using the AASM gem.

+3  A: 

What about using the :guard option to make sure the transition can be performed? If the guard method returns false, the transition isn't executed at all. So, I'd check if every condition of the transition is met with the guard method, first.

Milan Novota
+1  A: 

I've not used this plugin, but reading the code, unless I'm mistaken there doesn't appear to be any mechanism for communicating that either of these callbacks have failed.

This makes complete sense to me for the success callback, because it is only called once the transition has actually happened by which time it is too late to revert. If you want the exception raised within your success callback to prevent the transition, perhaps the code should go in the transition itself and not in the success callback...? Or maybe you need an extra state...?

Regarding the enter callback, would the guard callback not be more appropriate...? You can return false if the transition should not go ahead.

floehopper