如何跳过在 Rails 中记录未捕获的异常? [英] How to skip logging uncaught exceptions in Rails?

查看:33
本文介绍了如何跳过在 Rails 中记录未捕获的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用自定义 exceptions_apprescue_responses 时,应用程序可以更好地控制未捕获的异常,并且来自 DebugExceptions 中间件的过多日志记录会成为干扰.

When using custom exceptions_app and rescue_responses, an application has more control of uncaught exceptions and an excessive logging from DebugExceptions middleware becomes noise.

例如,应用程序知道如何处理ActionPolicy::Unauthorized,在exceptions_app 中呈现正确的页面,因此以下日志是多余的:

As an example, the application knows how to process ActionPolicy::Unauthorized, renders proper page in exceptions_app and thus the following log is redundant:

FATAL -- :   
FATAL -- : ActionPolicy::Unauthorized (Not Authorized):
FATAL -- :   
FATAL -- : app/controllers/topics_suggest_controller.rb:47:in `topic_load' 

跳过仅记录 rescue_responses 中列出的那些异常的最惯用的方法是什么?

What would be the most idiomatic way to skip logging only those exceptions listed in rescue_responses?

推荐答案

一些历史记录如下.截至 2021 年 6 月 25 日,我对 Rails 的 PR https://github.com/rails/rails/pull/42592 已被接受,此功能将在 Rails 6.1.5 中可用

Some historical notes are below. As of June 25, 2021, my PR to Rails https://github.com/rails/rails/pull/42592 has been accepted and this functionality will be available in Rails 6.1.5

很容易从应用程序的 Rails 堆栈中删除 DebugExceptions 中间件.

It's tempting to delete DebugExceptions middleware from the application's Rails stack.

config/application.rb:

config.middleware.delete ActionDispatch::DebugExceptions

问题在于,正是中间件决定了 Rails 找不到路由并在这种情况下抛出 ActionController::RoutingError.因此,如果您想在 exceptions_app 中对这个异常做出反应,这种方法对您来说是行不通的.

The problem is that it is exactly the middleware that determines Rails cannot find a route and throws ActionController::RoutingError in such cases. Hence, if you would like to react to this exception in your exceptions_app, this approach is a no-go for you.

如果您可以在未找到路由时看到 HTTP 状态 404 和纯文本响应 Not found,请继续使用此方法.

Go ahead and use this approach if it's fine for you to see HTTP status 404 and plain text response Not found when no route was found.

Monkey 以某种方式修补或更改 ActionDispatch::DebugExceptions.事实上,在 2013 年有一个提议改变 DebugExceptions 行为,以便它跳过在 rescue_responses 中注册的日志异常:9343,里面有示例代码.

Monkey patch or change ActionDispatch::DebugExceptions in some way. In fact, there was a proposal in 2013 to change DebugExceptions behavior so that it skips logging exceptions registered in rescue_responses: 9343, there is an example code in there.

我觉得覆盖整个类太多了,所以我选择覆盖它的方法 log_error 负责记录错误.

I think it's too much to overwrite the whole class, so I chose to override its method log_error responsible for logging errors.

lib/ext/suppress_exceptions.rb:

module Ext
  module SuppressExceptions
    private

    def log_error(_request, wrapper)
      exception = wrapper.exception
      return if ActionDispatch::ExceptionWrapper.rescue_responses.key? exception.class.name
      super
    end
  end
end

config/initializers/error_handling.rb:

require_dependency 'ext/suppress_exceptions'

ActiveSupport.on_load(:action_controller) do
  ActionDispatch::DebugExceptions.prepend Ext::SuppressExceptions
end

这篇关于如何跳过在 Rails 中记录未捕获的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆