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
This commit is contained in:
Brian Clozel 2018-11-03 21:28:48 +01:00
parent f42a653604
commit da53a0b8d5
4 changed files with 40 additions and 1 deletions

View File

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

View File

@ -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
public void invalidAcceptMediaType() {
this.contextRunner.run((context) -> {