我可以让 AASM 在事件失败时运行特定方法吗?

发布于 2024-08-13 17:40:17 字数 1135 浏览 2 评论 0原文

有没有一种好的方法可以告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-08-20 17:40:17

如果您使用 SimpleStateMachine 您可以执行以下操作:

  def something_risky
    something_that_might_raise_error
  rescue
    record_error
    raise #reraise the error
  end
  event :something_risky, :state1 => :state2,
                          RuntimeError => :failed

If you use SimpleStateMachine you can do:

  def something_risky
    something_that_might_raise_error
  rescue
    record_error
    raise #reraise the error
  end
  event :something_risky, :state1 => :state2,
                          RuntimeError => :failed
瑾兮 2024-08-20 17:40:17

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

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

I also had this problem. I had two things to do.

  1. Follow the suggestion in this blog http://degenportnoy.blogspot.com/2009/11/event-callbacks-in-aasm.html.
  2. You need to restart your app even if you are working on development mode.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文