Merge pull request #11909 from sdeleuze:improved-error-logging

* pr/11909:
  Polish
  Improve WebFlux error logging
This commit is contained in:
Brian Clozel 2018-02-13 18:22:34 +01:00
commit 45b63cb1b0
2 changed files with 34 additions and 3 deletions

View File

@ -39,6 +39,7 @@ import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ResponseStatusException;
/**
* Basic global {@link org.springframework.web.server.WebExceptionHandler}, rendering
@ -188,16 +189,43 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
}
/**
* Log the original exception if handling it results in a Server Error.
* Log the original exception if handling it results in a Server Error or a Bad Request
* (Client Error with 400 status code) one.
* @param request the source request
* @param errorStatus the HTTP error status
*/
protected void logError(ServerRequest request, HttpStatus errorStatus) {
if (errorStatus.is5xxServerError()) {
Throwable ex = getError(request);
logger.error("Failed to handle request [" + request.methodName() + " "
+ request.uri() + "]", ex);
if (ex instanceof ResponseStatusException) {
logger.error(buildMessage(request, ex));
}
else {
logger.error(buildMessage(request, null), ex);
}
}
else if (errorStatus == HttpStatus.BAD_REQUEST) {
Throwable ex = getError(request);
if (ex instanceof ResponseStatusException) {
logger.warn(buildMessage(request, ex));
}
else {
logger.warn(buildMessage(request, null), ex);
}
}
}
private String buildMessage(ServerRequest request, Throwable ex) {
StringBuilder message = new StringBuilder("Failed to handle request [");
message.append(request.methodName());
message.append(" ");
message.append(request.uri());
message.append("]");
if (ex != null) {
message.append(": ");
message.append(ex.getMessage());
}
return message.toString();
}
}

View File

@ -131,6 +131,9 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
.isEqualTo(("/bind")).jsonPath("exception").doesNotExist()
.jsonPath("errors").isArray().jsonPath("message").isNotEmpty();
});
this.output.expect(allOf(containsString("Failed to handle request [POST /bind]"),
containsString("Validation failed for argument"),
containsString("Field error in object 'dummyBody' on field 'content'")));
}
@Test