Skip to content

Commit 12b644d

Browse files
committed
Polish contribution
See gh-19901
1 parent aead3a7 commit 12b644d

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,39 +81,37 @@ public Map<String, Object> getErrorAttributes(ServerRequest request, boolean inc
8181
errorAttributes.put("timestamp", new Date());
8282
errorAttributes.put("path", request.path());
8383
Throwable error = getError(request);
84-
HttpStatus errorStatus = determineHttpStatus(error);
84+
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
85+
ResponseStatus.class);
86+
HttpStatus errorStatus = determineHttpStatus(error, responseStatus);
8587
errorAttributes.put("status", errorStatus.value());
8688
errorAttributes.put("error", errorStatus.getReasonPhrase());
87-
errorAttributes.put("message", determineMessage(error));
89+
errorAttributes.put("message", determineMessage(error, responseStatus));
8890
handleException(errorAttributes, determineException(error), includeStackTrace);
8991
return errorAttributes;
9092
}
9193

92-
private HttpStatus determineHttpStatus(Throwable error) {
94+
private HttpStatus determineHttpStatus(Throwable error, ResponseStatus responseStatus) {
9395
if (error instanceof ResponseStatusException) {
9496
return ((ResponseStatusException) error).getStatus();
9597
}
96-
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
97-
ResponseStatus.class);
9898
if (responseStatus != null) {
9999
return responseStatus.code();
100100
}
101101
return HttpStatus.INTERNAL_SERVER_ERROR;
102102
}
103103

104-
private String determineMessage(Throwable error) {
104+
private String determineMessage(Throwable error, ResponseStatus responseStatus) {
105105
if (error instanceof WebExchangeBindException) {
106106
return error.getMessage();
107107
}
108108
if (error instanceof ResponseStatusException) {
109109
return ((ResponseStatusException) error).getReason();
110110
}
111-
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
112-
ResponseStatus.class);
113-
if (responseStatus != null) {
111+
if (responseStatus != null && StringUtils.hasText(responseStatus.reason())) {
114112
return responseStatus.reason();
115113
}
116-
return error.getMessage();
114+
return (error.getMessage() != null) ? error.getMessage() : "";
117115
}
118116

119117
private Throwable determineException(Throwable error) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void annotatedResponseStatusCode() {
9494
}
9595

9696
@Test
97-
void annotatedResponseStatusCodeWithExceptionMessage() {
97+
public void annotatedResponseStatusCodeWithExceptionMessage() {
9898
Exception error = new CustomException("Test Message");
9999
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
100100
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error),

0 commit comments

Comments
 (0)