Skip to content

Commit fa239a0

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-20028
2 parents 90e3d88 + 29fb2a3 commit fa239a0

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.springframework.core.annotation.MergedAnnotations;
2727
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
2828
import org.springframework.http.HttpStatus;
29+
import org.springframework.util.StringUtils;
2930
import org.springframework.validation.BindingResult;
3031
import org.springframework.validation.ObjectError;
3132
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -108,7 +109,11 @@ private String determineMessage(Throwable error, MergedAnnotation<ResponseStatus
108109
if (error instanceof ResponseStatusException) {
109110
return ((ResponseStatusException) error).getReason();
110111
}
111-
return responseStatusAnnotation.getValue("reason", String.class).orElseGet(error::getMessage);
112+
String reason = responseStatusAnnotation.getValue("reason", String.class).orElse("");
113+
if (StringUtils.hasText(reason)) {
114+
return reason;
115+
}
116+
return (error.getMessage() != null) ? error.getMessage() : "";
112117
}
113118

114119
private Throwable determineException(Throwable error) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,6 +89,18 @@ void annotatedResponseStatusCode() {
8989
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error),
9090
false);
9191
assertThat(attributes.get("error")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
92+
assertThat(attributes.get("message")).isEqualTo("");
93+
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
94+
}
95+
96+
@Test
97+
void annotatedResponseStatusCodeWithExceptionMessage() {
98+
Exception error = new CustomException("Test Message");
99+
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
100+
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error),
101+
false);
102+
assertThat(attributes.get("error")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
103+
assertThat(attributes.get("message")).isEqualTo("Test Message");
92104
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
93105
}
94106

@@ -226,6 +238,13 @@ int method(String firstParam) {
226238
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
227239
static class CustomException extends RuntimeException {
228240

241+
CustomException() {
242+
}
243+
244+
CustomException(String message) {
245+
super(message);
246+
}
247+
229248
}
230249

231250
@ResponseStatus(value = HttpStatus.I_AM_A_TEAPOT, reason = "Nope!")

0 commit comments

Comments
 (0)