Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.DisconnectedClientHelper;
import org.springframework.web.util.HtmlUtils;
Expand All @@ -55,6 +56,7 @@
* @author Brian Clozel
* @author Scott Frederick
* @author Moritz Halbritter
* @author US JUN
* @since 4.0.0
* @see ErrorAttributes
*/
Expand Down Expand Up @@ -296,7 +298,7 @@ public Mono<Void> handle(ServerWebExchange exchange, Throwable throwable) {
.switchIfEmpty(Mono.error(throwable))
.flatMap((handler) -> handler.handle(request))
.doOnNext((response) -> logError(request, response, throwable))
.flatMap((response) -> write(exchange, response));
.flatMap((response) -> write(exchange, response, throwable));
}

private boolean isDisconnectedClientError(Throwable ex) {
Expand Down Expand Up @@ -333,7 +335,12 @@ private String formatRequest(ServerRequest request) {
return "HTTP " + request.method() + " \"" + request.path() + query + "\"";
}

private Mono<? extends Void> write(ServerWebExchange exchange, ServerResponse response) {
private Mono<? extends Void> write(ServerWebExchange exchange, ServerResponse response, Throwable throwable) {
if (throwable instanceof ResponseStatusException responseStatusException) {
responseStatusException.getHeaders()
.forEach((name, values) -> values
.forEach((value) -> exchange.getResponse().getHeaders().add(name, value)));
}
// force content-type since writeTo won't overwrite response header values
exchange.getResponse().getHeaders().setContentType(response.headers().getContentType());
return response.writeTo(exchange, new ResponseContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
Expand All @@ -75,6 +77,7 @@
*
* @author Brian Clozel
* @author Scott Frederick
* @author US JUN
*/
@ExtendWith(OutputCaptureExtension.class)
class DefaultErrorWebExceptionHandlerIntegrationTests {
Expand Down Expand Up @@ -381,6 +384,20 @@ void statusException() {
});
}

@Test // gh-18831
void responseStatusExceptionHeaders() {
this.contextRunner.run((context) -> {
WebTestClient client = getWebClient(context);
client.post()
.uri("/badRequest")
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.METHOD_NOT_ALLOWED)
.expectHeader()
.valueEquals(HttpHeaders.ALLOW, HttpMethod.GET.name());
});
}

@Test
void defaultErrorView() {
this.contextRunner
Expand Down