Make CodecException handling consistent
This commit makes CodecException handling consistent between functional and annotation-based APIs. It now returns by default 4xx status code for decoding error and 5xx for encoding error + print the error reason in logs without the full stack trace in both variants. Issue: SPR-15355
This commit is contained in:
@@ -237,6 +237,9 @@ public abstract class RouterFunctions {
|
||||
.otherwise(ResponseStatusException.class,
|
||||
ex -> {
|
||||
exchange.getResponse().setStatusCode(ex.getStatus());
|
||||
if (ex.getMessage() != null) {
|
||||
logger.error(ex.getMessage());
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,14 +22,11 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ReactiveAdapter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
@@ -154,7 +151,7 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
|
||||
}
|
||||
|
||||
private ServerWebInputException getReadError(MethodParameter parameter, Throwable ex) {
|
||||
return new ServerWebInputException("Failed to read HTTP message", parameter, ex);
|
||||
return new ServerWebInputException("Failed to read HTTP message", parameter, ex instanceof ResponseStatusException ? ex.getCause() : ex);
|
||||
}
|
||||
|
||||
private ServerWebInputException getRequiredBodyError(MethodParameter parameter) {
|
||||
|
||||
@@ -102,7 +102,7 @@ public class DispatcherHandlerErrorTests {
|
||||
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
|
||||
|
||||
StepVerifier.create(publisher)
|
||||
.consumeErrorWith(error -> assertSame(EXCEPTION, error))
|
||||
.consumeErrorWith(error -> assertSame(EXCEPTION, error.getCause()))
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user