gpt4 book ai didi

spring - 将某些异常排除在报告给 Sentry 之外

转载 作者:行者123 更新时间:2023-12-02 13:09:22 41 4
gpt4 key购买 nike

docs 中所述,基于 spring-boot 的 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();
}
}

关于spring - 将某些异常排除在报告给 Sentry 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037108/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com