Spring 不包括向哨兵报告的某些例外情况

Spring 不包括向哨兵报告的某些例外情况,spring,spring-boot,exception,kotlin,sentry,Spring,Spring Boot,Exception,Kotlin,Sentry,基于spring引导的web服务正在使用Sentry,如中所述。它可以正常工作,但有些异常不应发送给Sentry,例如,在某些请求上,为了返回HTTP状态而引发的异常410: // Kotlin code, but in Java it would be similar. @ResponseStatus(value = HttpStatus.GONE) class GoneException(msg: String) : RuntimeException(msg) { } 如何让我的sentr

基于spring引导的web服务正在使用Sentry,如中所述。它可以正常工作,但有些异常不应发送给Sentry,例如,在某些请求上,为了返回HTTP状态而引发的异常
410

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

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

在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();
    }
}

在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\u异常的东西。有人能详细说明一下
while(rootCause.getCause()!=null和&rootCause.getCause()!=rootCause){
部分吗?很明显,它会深入查找异常()的根本原因,但因为OP的目标是“top”级别异常我不确定该部分是否必要。谢谢!非常感谢。我也没有发现类似于
ignore\u异常的东西。因此我和它可以工作。请有人详细说明
while(rootCause.getCause()!=null&&rootCause.getCause()!=rootCause){
part?很明显,它会深入查找异常()的根本原因,但由于OP针对的是“顶级”异常,我不确定该部分是否必要。谢谢!