Commit 247b7f08 authored by Brian Clozel's avatar Brian Clozel

Fix WebFlux default error view for null exception messages

This commit prevents NullPointerExceptions when the default HTML error
view is being rendered with a `null` message.

Fixes gh-11677
parent 0b81f78a
...@@ -195,9 +195,13 @@ public abstract class AbstractErrorWebExceptionHandler ...@@ -195,9 +195,13 @@ public abstract class AbstractErrorWebExceptionHandler
.append(HtmlUtils.htmlEscape(error.get("error").toString())) .append(HtmlUtils.htmlEscape(error.get("error").toString()))
.append(", status=") .append(", status=")
.append(HtmlUtils.htmlEscape(error.get("status").toString())) .append(HtmlUtils.htmlEscape(error.get("status").toString()))
.append(").</div>").append("<div>") .append(").</div>");
.append(HtmlUtils.htmlEscape(error.get("message").toString())) if (error.get("message") != null) {
.append("</div>").append("</body></html>"); builder.append("<div>")
.append(HtmlUtils.htmlEscape(error.get("message").toString()))
.append("</div>");
}
builder.append("</body></html>");
return responseBody.syncBody(builder.toString()); return responseBody.syncBody(builder.toString());
} }
......
...@@ -240,6 +240,23 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests { ...@@ -240,6 +240,23 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
}); });
} }
@Test
public void testExceptionWithNullMessage() throws Exception {
this.contextRunner
.withPropertyValues("spring.mustache.prefix=classpath:/unknown/")
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/notfound").accept(MediaType.TEXT_HTML)
.exchange().expectStatus()
.isEqualTo(HttpStatus.NOT_FOUND).expectHeader()
.contentType(MediaType.TEXT_HTML).expectBody(String.class)
.returnResult().getResponseBody();
assertThat(body).contains("Whitelabel Error Page")
.contains("type=Not Found, status=404");
});
}
@Test @Test
public void responseCommitted() throws Exception { public void responseCommitted() throws Exception {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment