I built a server following Webflux functional programming, and added below code to my Router: route(GET("/test/{Id}"), request -> throw new RuntimeException("123"))
.
But when I call /test/{Id}
, the only error log in console is:
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
No stack trace, but why? It should be handled by spring or netty, not my customized code, right? Setting logging.level.org.springframework.web: trace
is not a solution, there're too many logs.
I've checked why spring mvc has stack trace, because there is a log.error in try-catch in tomcat and it's proven by debugging.
Then I thought does Netty has these logic too? Actually it has! But what's confuse me is that I can't pause the code in this try-catch with any breakpoints.
Which means there may exists some Mono.onErrorResume
swallowing the exception, so netty can't catch anything. But I don't know how to debug a large Mono to check the root cause. And why swallow it?
Option 1A: Set application properties as follows:
server.error.includeStacktrace=ALWAYS
Option 1B: Set application properties as follows:
server.error.includeStacktrace=ON_TRACE_PARAM
And, specify request parameter trace
to true
.
Option 2: Add a customized WebExceptionHandler, and make sure it's in the component scan scope.
@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);
}
}
See https://www.baeldung.com/spring-webflux-errors for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With