Fix HTTP status error template rendering in WebFlux

Prior to this commit, a change in `HttpStatus.toString` since SPR-16898
prevented the default WebFlux `ErrorWebExceptionHandler` to render
template views for exact HTTP status (e.g. "404.html").
This issue does not affect the resolution of series, like "4xx.html".

This commit fixes `DefaultErrorWebExceptionHandler` to use
`HttpStatus.value()` when attempting to resolve error views.

Closes gh-15083
pull/15084/head
Brian Clozel 6 years ago
parent f42a653604
commit da53a0b8d5

@ -122,7 +122,7 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
ServerResponse.BodyBuilder responseBody = ServerResponse.status(errorStatus) ServerResponse.BodyBuilder responseBody = ServerResponse.status(errorStatus)
.contentType(MediaType.TEXT_HTML); .contentType(MediaType.TEXT_HTML);
return Flux return Flux
.just("error/" + errorStatus.toString(), .just("error/" + errorStatus.value(),
"error/" + SERIES_VIEWS.get(errorStatus.series()), "error/error") "error/" + SERIES_VIEWS.get(errorStatus.series()), "error/error")
.flatMap((viewName) -> renderErrorView(viewName, responseBody, error)) .flatMap((viewName) -> renderErrorView(viewName, responseBody, error))
.switchIfEmpty(this.errorProperties.getWhitelabel().isEnabled() .switchIfEmpty(this.errorProperties.getWhitelabel().isEnabled()

@ -257,6 +257,43 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
}); });
} }
@Test
public void exactStatusTemplateErrorPage() {
this.contextRunner
.withPropertyValues("server.error.whitelabel.enabled=false",
"spring.mustache.prefix=" + getErrorTemplatesLocation())
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/notfound")
.accept(MediaType.TEXT_HTML).exchange().expectStatus()
.isNotFound().expectBody(String.class).returnResult()
.getResponseBody();
assertThat(body).contains("404 page");
});
}
@Test
public void seriesStatusTemplateErrorPage() {
this.contextRunner
.withPropertyValues("server.error.whitelabel.enabled=false",
"spring.mustache.prefix=" + getErrorTemplatesLocation())
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/badRequest")
.accept(MediaType.TEXT_HTML).exchange().expectStatus()
.isBadRequest().expectBody(String.class).returnResult()
.getResponseBody();
assertThat(body).contains("4xx page");
});
}
private String getErrorTemplatesLocation() {
String packageName = getClass().getPackage().getName();
return "classpath:/" + packageName.replace('.', '/') + "/templates/";
}
@Test @Test
public void invalidAcceptMediaType() { public void invalidAcceptMediaType() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {

Loading…
Cancel
Save