Improve WebFlux support for response status exceptions
Support @ResponseStatus annotated exceptions. Supported root cause exceptions with response status information. Issue: SPR-16567
This commit is contained in:
@@ -20,14 +20,22 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebExceptionHandler;
|
||||
|
||||
/**
|
||||
* Handle {@link ResponseStatusException} by setting the response status.
|
||||
* Handle instances of {@link ResponseStatusException}, or of exceptions annotated
|
||||
* with {@link ResponseStatus @ResponseStatus}, by extracting the
|
||||
* {@code HttpStatus} from them and updating the status of the response.
|
||||
*
|
||||
* <p>If the response is already committed, the error remains unresolved and is
|
||||
* propagated.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
@@ -40,24 +48,37 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
||||
if (ex instanceof ResponseStatusException) {
|
||||
HttpStatus status = ((ResponseStatusException) ex).getStatus();
|
||||
if (exchange.getResponse().setStatusCode(status)) {
|
||||
if (status.is5xxServerError()) {
|
||||
logger.error(buildMessage(exchange.getRequest(), ex));
|
||||
}
|
||||
else if (status == HttpStatus.BAD_REQUEST) {
|
||||
logger.warn(buildMessage(exchange.getRequest(), ex));
|
||||
}
|
||||
else {
|
||||
logger.trace(buildMessage(exchange.getRequest(), ex));
|
||||
}
|
||||
return exchange.getResponse().setComplete();
|
||||
HttpStatus status = resolveHttpStatus(ex);
|
||||
if (status != null && exchange.getResponse().setStatusCode(status)) {
|
||||
if (status.is5xxServerError()) {
|
||||
logger.error(buildMessage(exchange.getRequest(), ex));
|
||||
}
|
||||
else if (status == HttpStatus.BAD_REQUEST) {
|
||||
logger.warn(buildMessage(exchange.getRequest(), ex));
|
||||
}
|
||||
else {
|
||||
logger.trace(buildMessage(exchange.getRequest(), ex));
|
||||
}
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
return Mono.error(ex);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private HttpStatus resolveHttpStatus(Throwable ex) {
|
||||
if (ex instanceof ResponseStatusException) {
|
||||
return ((ResponseStatusException) ex).getStatus();
|
||||
}
|
||||
ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);
|
||||
if (status != null) {
|
||||
return status.code();
|
||||
}
|
||||
if (ex.getCause() != null) {
|
||||
return resolveHttpStatus(ex.getCause());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String buildMessage(ServerHttpRequest request, Throwable ex) {
|
||||
return "Failed to handle request [" + request.getMethod() + " "
|
||||
+ request.getURI() + "]: " + ex.getMessage();
|
||||
|
||||
Reference in New Issue
Block a user