我在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具有堆栈跟踪,因为tomcat中有一个log.error in try-catch并且已经通过调试证明了。
那我想Netty也有这些逻辑吗?实际上是has!但是令我困惑的是,我无法使用任何断点暂停此try-catch中的代码。
这意味着可能存在一些Mono.onErrorResume
吞没了该异常,因此netty无法捕获任何东西。但我不知道如何调试大型Mono以检查根本原因。为什么要吞下去?
答案 0 :(得分:1)
选项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。