Skip to content

Commit 29fb2a3

Browse files
committed
Merge pull request #19901 from nosan
* pr/19901: Polish contribution Handle message of @ResponseStatus-annotated exception with WebFlux Closes gh-19901
2 parents c8a5106 + 12b644d commit 29fb2a3

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

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

Lines changed: 10 additions & 11 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.
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.core.annotation.AnnotatedElementUtils;
2626
import org.springframework.http.HttpStatus;
27+
import org.springframework.util.StringUtils;
2728
import org.springframework.validation.BindingResult;
2829
import org.springframework.validation.ObjectError;
2930
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -80,39 +81,37 @@ public Map<String, Object> getErrorAttributes(ServerRequest request, boolean inc
8081
errorAttributes.put("timestamp", new Date());
8182
errorAttributes.put("path", request.path());
8283
Throwable error = getError(request);
83-
HttpStatus errorStatus = determineHttpStatus(error);
84+
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
85+
ResponseStatus.class);
86+
HttpStatus errorStatus = determineHttpStatus(error, responseStatus);
8487
errorAttributes.put("status", errorStatus.value());
8588
errorAttributes.put("error", errorStatus.getReasonPhrase());
86-
errorAttributes.put("message", determineMessage(error));
89+
errorAttributes.put("message", determineMessage(error, responseStatus));
8790
handleException(errorAttributes, determineException(error), includeStackTrace);
8891
return errorAttributes;
8992
}
9093

91-
private HttpStatus determineHttpStatus(Throwable error) {
94+
private HttpStatus determineHttpStatus(Throwable error, ResponseStatus responseStatus) {
9295
if (error instanceof ResponseStatusException) {
9396
return ((ResponseStatusException) error).getStatus();
9497
}
95-
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
96-
ResponseStatus.class);
9798
if (responseStatus != null) {
9899
return responseStatus.code();
99100
}
100101
return HttpStatus.INTERNAL_SERVER_ERROR;
101102
}
102103

103-
private String determineMessage(Throwable error) {
104+
private String determineMessage(Throwable error, ResponseStatus responseStatus) {
104105
if (error instanceof WebExchangeBindException) {
105106
return error.getMessage();
106107
}
107108
if (error instanceof ResponseStatusException) {
108109
return ((ResponseStatusException) error).getReason();
109110
}
110-
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
111-
ResponseStatus.class);
112-
if (responseStatus != null) {
111+
if (responseStatus != null && StringUtils.hasText(responseStatus.reason())) {
113112
return responseStatus.reason();
114113
}
115-
return error.getMessage();
114+
return (error.getMessage() != null) ? error.getMessage() : "";
116115
}
117116

118117
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 @@ public 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+
public 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

@@ -218,6 +230,13 @@ public int method(String firstParam) {
218230
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
219231
private static class CustomException extends RuntimeException {
220232

233+
CustomException() {
234+
}
235+
236+
CustomException(String message) {
237+
super(message);
238+
}
239+
221240
}
222241

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

0 commit comments

Comments
 (0)