有没有一种好方法可以告诉 AASM,如果在处理任何 assm_event 时引发异常,我希望该错误被特定的代码块捕获?
例如,目前我正在做类似的事情
assm_state :state_1
assm_state :state_2, :before_enter => :validate_something
assm_state :failed
assm_event :something_risky do
transition :from => :state_1, :to => :state_2
end
assm_event :fail do
transition :from => [:state_1, :state_2], :to => :failed
end
def validate_something
begin
something_that_might_raise_error
rescue
self.record_error
self.fail
end
end
我更愿意做的是
assm_state :state_1
assm_state :state_2, :before_enter => :validate_something
assm_state :failed
assm_event :something_risky, :on_exception => :log_failure do
transition :from => :state_1, :to => :state_2
end
assm_event :fail do
transition :from => [:state_1, :state_2], :to => :failed
end
def validate_something
something_that_might_raise_exception
end
def log_failure
self.record_error
self.fail
end
如果确实引发了异常,并且已经log_failure
被调用。something_that_might_raise_exception
理想情况下,我想避免更改 AASM,因此如果将来需要升级它,我是安全的