问题
我在 Webflux functional programming 之后构建了一个服务器,并将以下代码添加到我的路由器:route(GET("/test/{Id}"), request -> throw new RuntimeException("123"))
.
但是当我调用 /test/{Id}
,控制台中唯一的错误日志是:
TRACE 153036 --- [ctor-http-nio-7] o.s.w.r.function.server.RouterFunctions : [fc7e809d] Matched org.springframework.web.reactive.function.server.RequestPredicates$$Lambda$827/1369035321@9d8c274
DEBUG 153036 --- [ctor-http-nio-7] org.springframework.web.HttpLogging : [fc7e809d] Resolved [RuntimeException: 123] for HTTP GET /test/job
TRACE 153036 --- [ctor-http-nio-7] org.springframework.web.HttpLogging : [fc7e809d] Encoding [{timestamp=Mon Dec 17 15:34:43 CST 2018, path=/test/123, status=500, error=Internal Server Error, message=123}]
TRACE 153036 --- [ctor-http-nio-7] o.s.w.s.adapter.HttpWebHandlerAdapter : [fc7e809d] Completed 500 INTERNAL_SERVER_ERROR, headers={masked}
TRACE 153036 --- [ctor-http-nio-7] org.springframework.web.HttpLogging : [fc7e809d] Handling completed
没有堆栈跟踪,但为什么呢? 应该是spring或者netty处理的,不是我自定义的代码吧?设置logging.level.org.springframework.web: trace
不是解决方案,日志太多。这是我到目前为止发现的,但仍然很困惑:
我检查了为什么 spring mvc 有堆栈跟踪,因为有一个 log.error in try-catch在tomcat中,并通过调试证明。
然后我想Netty也有这些逻辑吗?其实是has !但是让我感到困惑的是,我不能在这个 try-catch 中用任何断点暂停代码。
这意味着可能存在一些
Mono.onErrorResume
吞下异常,所以 netty 无法捕获任何东西。但我不知道如何调试大型 Mono 来检查根本原因。为什么要吞下去?
最佳答案
选项 1A:设置应用程序属性如下:
server.error.includeStacktrace=ALWAYS
选项 1B:设置应用程序属性如下:
server.error.includeStacktrace=ON_TRACE_PARAM
和 , 指定请求参数
trace
至true
.选项 2:添加自定义 WebExceptionHandler,并确保它在组件扫描范围内。
@Component
@Order(-2)
public class LoggingErrorWebExceptionHandler extends DefaultErrorWebExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(LoggingErrorWebExceptionHandler.class);
public LoggingErrorWebExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
ServerProperties serverProperties, ApplicationContext applicationContext, ServerCodecConfigurer serverCodecConfigurer) {
super(errorAttributes, resourceProperties, serverProperties.getError(), applicationContext);
super.setMessageWriters(serverCodecConfigurer.getWriters());
super.setMessageReaders(serverCodecConfigurer.getReaders());
}
@Override
protected void logError(ServerRequest request, HttpStatus errorStatus) {
Throwable ex = getError(request);
logger.error("Error Occurred:", ex);
super.logError(request, errorStatus);
}
}
见 https://www.baeldung.com/spring-webflux-errors更多细节。
关于spring - 为什么默认配置的spring webflux中没有异常堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53810900/