我可以让 AASM 在事件失败时运行特定方法吗?
有没有一种好的方法可以告诉 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
如果 something_that_might_raise_exception
确实引发异常,则调用 log_failure
。理想情况下,我想避免更改 AASM,这样如果将来需要升级它,我就可以安全了
Is there a nice way to tell AASM that if an exception is raised while processing any assm_event I want that error to be caught by a specific block of code?
eg currently I do something like
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
and what I would prefer to do is something like
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
and have log_failure
be called if something_that_might_raise_exception
does raise an exception. Ideally I want to avoid changing AASM so I am safe if I need to upgrade it in the future
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 SimpleStateMachine 您可以执行以下操作:
If you use SimpleStateMachine you can do:
我也遇到了这个问题。我有两件事要做。
I also had this problem. I had two things to do.