From 247b7f0842b4b902b6dafae58bf1228e5668f99c Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 19 Jan 2018 10:13:59 +0100 Subject: [PATCH] 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 --- .../error/AbstractErrorWebExceptionHandler.java | 10 +++++++--- ...rrorWebExceptionHandlerIntegrationTests.java | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java index 5fcdc93e4b..063c67b778 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java @@ -195,9 +195,13 @@ public abstract class AbstractErrorWebExceptionHandler .append(HtmlUtils.htmlEscape(error.get("error").toString())) .append(", status=") .append(HtmlUtils.htmlEscape(error.get("status").toString())) - .append(").").append("
") - .append(HtmlUtils.htmlEscape(error.get("message").toString())) - .append("
").append(""); + .append(")."); + if (error.get("message") != null) { + builder.append("
") + .append(HtmlUtils.htmlEscape(error.get("message").toString())) + .append("
"); + } + builder.append(""); return responseBody.syncBody(builder.toString()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java index d259064bba..e963875d3d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java @@ -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 public void responseCommitted() throws Exception { this.contextRunner.run((context) -> {