【问题标题】:Notify Airbrake when capturing an exception and manually logging the error捕获异常并手动记录错误时通知 Airbrake
【发布时间】:2020-07-30 19:53:22
【问题描述】:

在我们的 Rails 应用程序中,我们使用 Airbrake 连接到托管的 Errbit 服务器。

我们在很多地方都使用rescuerescue_from,希望以特定方式处理任何异常,然后我们在返回响应之前自己记录异常。

我们在ApplicationController 中有一些示例:

  rescue_from CanCan::AccessDenied do |e|
    Rails.logger.error "CanCan exception: #{e.message}"
    render 'errors/401', status: 401
  end

  rescue_from ActionController::InvalidAuthenticityToken do |e|
    Rails.logger.error "Authenticity exception: #{e.message}"
    render 'errors/csrf', status: 400
  end

然后我们还有一些单独的方法,例如 API:

def try_request
  Response.new(yield)
rescue RestClient::Unauthorized,
       RestClient::ExceptionWithResponse,
       RestClient::InternalServerError,
       RestClient::BadRequest => e
  Rails.logger.error "API exception: #{e.message}"
  Response.new(e.response)
end

但是,通过使用rescue,我们注意到 Errbit 不再接收我们的异常,因为我们正在捕获它们......这很有意义......但我们仍然希望在 Errbit 报告中看到这些!

您可以通过以下方式手动通知 Airbrake:

Airbrake.notify(e)

但我们希望避免将这段代码放在每个救援块中。

是否可以有一个更高级别的语句,可以在手动记录错误时自动通知 Airbrake?也许是在调用Rails.logger.error 时调用的中间件?

【问题讨论】:

    标签: ruby-on-rails rescue airbrake


    【解决方案1】:

    rescue 是 ruby​​ 子句,你不能直接覆盖它(即使你可以 - 这不是一个好主意)

    在你所有的例子中都有Rails.logger.error,你可以让自己成为一个助手,比如:

    module MyErrorNotifier
      def self.notify(message, exception, params = {}, &block)
        Rails.logger.error("#{message}: #{exception.message}")
        Airbrake.notify(exception, params, &block)
      end
    end
    
    

    而不是记录器调用它:

    def try_request
     ...
    rescue e
      MyErrorNotifier.notify("Foo exception happened", e)
      Response.new(e.response)
    end
    

    【讨论】:

    • 我在想的是在进行实际记录时自动告诉空气制动而不是覆盖救援......例如也许是某种中间件?
    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 2010-12-21
    • 2019-08-11
    • 2018-10-22
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2015-06-11
    相关资源
    最近更新 更多