Skip to content

Commit aead3a7

Browse files
nosansnicoll
authored andcommitted
Handle message of @ResponseStatus-annotated exception with WebFlux
See gh-19901
1 parent c8a5106 commit aead3a7

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 2 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.
@@ -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;

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+
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)