【问题标题】:Excluding certain exceptions from being reported to Sentry不向 Sentry 报告某些例外情况
【发布时间】:2018-10-06 19:20:59
【问题描述】:

一个基于 spring-boot 的 web 服务正在使用 docs 中描述的 Sentry。它工作正常,但不应将某些异常发送到 Sentry,例如为了在某​​些请求上返回 HTTP 状态 410 而引发的异常:

// Kotlin code, but in Java it would be similar.
@ResponseStatus(value = HttpStatus.GONE)
class GoneException(msg: String) : RuntimeException(msg) {
}

如何告诉我的sentryExceptionResolver 跳过这些异常?

【问题讨论】:

    标签: spring spring-boot exception kotlin sentry


    【解决方案1】:

    在 python 中很简单,您只需在配置文件中添加以下代码即可忽略多个异常

    ignore_exceptions = [
        'Http404',
        'Http401'
        'django.exceptions.http.Http404',
        'django.exceptions.*',
        ValueError,
    ]
    

    但是在java中我在sentry.properties中找不到类似的标签,你自己试试也许你会找到。

    ##Just give it a try, I didnt test    
    ignore.exceptions:
            HTTP 401
    

    您可以在配置类中添加HandlerExceptionResolver 并覆盖resolveException 方法并手动忽略异常。

    @Configuration
    public class FactoryBeanAppConfig {
        @Bean
        public HandlerExceptionResolver sentryExceptionResolver() {
            return new SentryExceptionResolver() {
                @Override
                public ModelAndView resolveException(HttpServletRequest request,
                        HttpServletResponse response,
                        Object handler,
                        Exception ex) {
                    Throwable rootCause = ex;
    
                    while (rootCause .getCause() != null && rootCause.getCause() != rootCause) {
                        rootCause = rootCause.getCause();
                    }
    
                    if (!rootCause.getMessage().contains("HTTP 401")) {
                        super.resolveException(request, response, handler, ex);
                    }
                    return null;
                }   
    
            };
        }
    
        @Bean
        public ServletContextInitializer sentryServletContextInitializer() {
            return new SentryServletContextInitializer();
        }
    }
    

    【讨论】:

    • 非常感谢。我也没有找到像ignore_exception 这样的东西。所以我translated your code to Kotlin 并且它有效。
    • 有人可以详细说明while (rootCause .getCause() != null && rootCause.getCause() != rootCause) { 部分吗?很明显,它会深入查找异常的根本原因 (reference),但由于 OP 的目标是“顶级”异常,我不确定该部分是否必要。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 2019-01-22
    • 2020-06-20
    • 2021-07-20
    • 2020-06-19
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多