spring 排除向哨兵报告的某些例外情况

2izufjch  于 2023-01-24  发布在  Spring
关注(0)|答案(2)|浏览(142)

一个基于Spring启动的Web服务正在使用Sentry,如docs中所述。它工作正常,但有些异常不应发送到Sentry,例如,在某些请求上为了返回HTTP状态410而抛出的异常:

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

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

xt0899hw

xt0899hw1#

在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

或者您可以在Configuration类中添加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();
    }
}
1l5u6lss

1l5u6lss2#

您可以设置;

ignored-exceptions-for-type=java.lang.RuntimeException,java.lang.IllegalStateException

https://docs.sentry.io/platforms/java/configuration/

相关问题