Skip to content

Commit 144ca05

Browse files
committed
Merge pull request #14744 from mmanciop
* pr/14744: Polish "Add support for @ResponseStatus in DefaultErrorAttributes" Add support for @ResponseStatus in DefaultErrorAttributes
2 parents c0a5e98 + 798b378 commit 144ca05

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.Map;
2424

25+
import org.springframework.core.annotation.AnnotatedElementUtils;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.validation.BindingResult;
2728
import org.springframework.validation.ObjectError;
29+
import org.springframework.web.bind.annotation.ResponseStatus;
2830
import org.springframework.web.bind.support.WebExchangeBindException;
2931
import org.springframework.web.reactive.function.server.ServerRequest;
3032
import org.springframework.web.server.ResponseStatusException;
@@ -46,6 +48,7 @@
4648
*
4749
* @author Brian Clozel
4850
* @author Stephane Nicoll
51+
* @author Michele Mancioppi
4952
* @since 2.0.0
5053
* @see ErrorAttributes
5154
*/
@@ -91,6 +94,11 @@ private HttpStatus determineHttpStatus(Throwable error) {
9194
if (error instanceof ResponseStatusException) {
9295
return ((ResponseStatusException) error).getStatus();
9396
}
97+
ResponseStatus responseStatus = AnnotatedElementUtils
98+
.findMergedAnnotation(error.getClass(), ResponseStatus.class);
99+
if (responseStatus != null) {
100+
return responseStatus.code();
101+
}
94102
return HttpStatus.INTERNAL_SERVER_ERROR;
95103
}
96104

@@ -101,6 +109,11 @@ private String determineMessage(Throwable error) {
101109
if (error instanceof ResponseStatusException) {
102110
return ((ResponseStatusException) error).getReason();
103111
}
112+
ResponseStatus responseStatus = AnnotatedElementUtils
113+
.findMergedAnnotation(error.getClass(), ResponseStatus.class);
114+
if (responseStatus != null) {
115+
return responseStatus.reason();
116+
}
104117
return error.getMessage();
105118
}
106119

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.validation.BindingResult;
3636
import org.springframework.validation.MapBindingResult;
3737
import org.springframework.validation.ObjectError;
38+
import org.springframework.web.bind.annotation.ResponseStatus;
3839
import org.springframework.web.bind.support.WebExchangeBindException;
3940
import org.springframework.web.reactive.function.server.ServerRequest;
4041
import org.springframework.web.server.ResponseStatusException;
@@ -90,6 +91,29 @@ public void defaultStatusCode() {
9091
assertThat(attributes.get("status")).isEqualTo(500);
9192
}
9293

94+
@Test
95+
public void annotatedResponseStatusCode() {
96+
Exception error = new CustomException();
97+
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
98+
Map<String, Object> attributes = this.errorAttributes
99+
.getErrorAttributes(buildServerRequest(request, error), false);
100+
assertThat(attributes.get("error"))
101+
.isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
102+
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
103+
}
104+
105+
@Test
106+
public void annotatedResponseStatusCodeWithCustomReasonPhrase() {
107+
Exception error = new Custom2Exception();
108+
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
109+
Map<String, Object> attributes = this.errorAttributes
110+
.getErrorAttributes(buildServerRequest(request, error), false);
111+
assertThat(attributes.get("error"))
112+
.isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
113+
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
114+
assertThat(attributes.get("message")).isEqualTo("Nope!");
115+
}
116+
93117
@Test
94118
public void includeStatusCode() {
95119
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
@@ -214,4 +238,14 @@ public int method(String firstParam) {
214238
return 42;
215239
}
216240

241+
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
242+
private static class CustomException extends RuntimeException {
243+
244+
}
245+
246+
@ResponseStatus(value = HttpStatus.I_AM_A_TEAPOT, reason = "Nope!")
247+
private static class Custom2Exception extends RuntimeException {
248+
249+
}
250+
217251
}

0 commit comments

Comments
 (0)