对模型问题使用救援_from

发布于 2025-01-13 22:01:52 字数 716 浏览 5 评论 0原文

我想对模型问题使用救援_from 方法,但没有将异常错误救援到我指定的方法:

require "active_support/concern"

module CustomErrorHandler
    extend ActiveSupport::Concern
    include ActiveSupport::Rescuable
    
    included do
        rescue_from StandardError, with: :capture_error
    end
    
    
    private
    
    def capture_error(e)
        puts "Error catched!"
    end
end

class FooClass
    include CustomErrorHandler
    
    def some_action
        raise StandardError, 'Error'
    end
end

err = FooClass.new
err.some_action

Traceback (most recent call last):
        2: from (irb):28
        1: from (irb):23:in `some_action'
StandardError (Error) # Didn't catch the error!

任何人都可以帮助我解决这个问题吗?谢谢!

I want to use rescue_from method on a model concern but neither exception error is rescued to the method i specified:

require "active_support/concern"

module CustomErrorHandler
    extend ActiveSupport::Concern
    include ActiveSupport::Rescuable
    
    included do
        rescue_from StandardError, with: :capture_error
    end
    
    
    private
    
    def capture_error(e)
        puts "Error catched!"
    end
end

class FooClass
    include CustomErrorHandler
    
    def some_action
        raise StandardError, 'Error'
    end
end

err = FooClass.new
err.some_action

Traceback (most recent call last):
        2: from (irb):28
        1: from (irb):23:in `some_action'
StandardError (Error) # Didn't catch the error!

Anyone can help me on how can i solve this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

给妤﹃绝世温柔 2025-01-20 22:01:52

这个想法从根本上就被打破了。

rescue_from 基本上只是语法糖:

class ThingsController < ApplicationController
  around_action :wrap_in_a_rescue

  def show
    raise SomeKindOfException
  end

  private

  def wrap_in_a_rescue
    begin
      yield
    rescue SomeKindOfException
      do_something_else
    end
  end

  def do_something_else
    render plain: 'Oh Noes!'
  end
end

它仅在控制器声明回调时才有效。

它的使用在控制器中很有意义,因为它可以挽救回调以及方法本身中发生的异常,并且可以与继承一起使用来自定义错误响应。在该上下文之外,它的使用没有多大意义。

虽然您可以通过包含 ActiveSupport::Callbacks 并声明必要的回调,这很可能是对原始问题的非常复杂的解决方案,很可能可以通过简单的救援或组合来处理。

This idea is fundamentially broken.

rescue_from is basically just syntactic sugar for:

class ThingsController < ApplicationController
  around_action :wrap_in_a_rescue

  def show
    raise SomeKindOfException
  end

  private

  def wrap_in_a_rescue
    begin
      yield
    rescue SomeKindOfException
      do_something_else
    end
  end

  def do_something_else
    render plain: 'Oh Noes!'
  end
end

It only works because the controller declares a callback.

Its use makes sense in a controller as it rescues exceptions that occur in its callbacks as well as in the method itself and can be used with inheritiance to customize the error responses. It use does not make very much sense outside of that context.

While you could maybe fix this code by including ActiveSupport::Callbacks and declaring the requisite callbacks it is most likely a very overcomplicated solution to the original problem which can most likely be handled with a simple rescue or composition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文