1

有没有一种好方法可以告诉 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,因此如果将来需要升级它,我是安全的

4

2 回答 2

0

如果你使用SimpleStateMachine你可以这样做:

  def something_risky
    something_that_might_raise_error
  rescue
    record_error
    raise #reraise the error
  end
  event :something_risky, :state1 => :state2,
                          RuntimeError => :failed
于 2010-09-07T07:43:47.560 回答
0

我也有这个问题。我有两件事要做。

  1. 遵循此博客http://degenportnoy.blogspot.com/2009/11/event-callbacks-in-aasm.html中的建议。
  2. 即使您在开发模式下工作,您也需要重新启动您的应用程序。
于 2011-11-20T07:01:26.357 回答