Merge pull request #14744 from mmanciop

* pr/14744:
  Polish "Add support for @ResponseStatus in DefaultErrorAttributes"
  Add support for @ResponseStatus in DefaultErrorAttributes
pull/14914/head
Stephane Nicoll 6 years ago
commit 144ca05cc1

@ -22,9 +22,11 @@ import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError; import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException; import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
@ -46,6 +48,7 @@ import org.springframework.web.server.ServerWebExchange;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Michele Mancioppi
* @since 2.0.0 * @since 2.0.0
* @see ErrorAttributes * @see ErrorAttributes
*/ */
@ -91,6 +94,11 @@ public class DefaultErrorAttributes implements ErrorAttributes {
if (error instanceof ResponseStatusException) { if (error instanceof ResponseStatusException) {
return ((ResponseStatusException) error).getStatus(); return ((ResponseStatusException) error).getStatus();
} }
ResponseStatus responseStatus = AnnotatedElementUtils
.findMergedAnnotation(error.getClass(), ResponseStatus.class);
if (responseStatus != null) {
return responseStatus.code();
}
return HttpStatus.INTERNAL_SERVER_ERROR; return HttpStatus.INTERNAL_SERVER_ERROR;
} }
@ -101,6 +109,11 @@ public class DefaultErrorAttributes implements ErrorAttributes {
if (error instanceof ResponseStatusException) { if (error instanceof ResponseStatusException) {
return ((ResponseStatusException) error).getReason(); return ((ResponseStatusException) error).getReason();
} }
ResponseStatus responseStatus = AnnotatedElementUtils
.findMergedAnnotation(error.getClass(), ResponseStatus.class);
if (responseStatus != null) {
return responseStatus.reason();
}
return error.getMessage(); return error.getMessage();
} }

@ -35,6 +35,7 @@ import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.MapBindingResult; import org.springframework.validation.MapBindingResult;
import org.springframework.validation.ObjectError; import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException; import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
@ -90,6 +91,29 @@ public class DefaultErrorAttributesTests {
assertThat(attributes.get("status")).isEqualTo(500); assertThat(attributes.get("status")).isEqualTo(500);
} }
@Test
public void annotatedResponseStatusCode() {
Exception error = new CustomException();
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Map<String, Object> attributes = this.errorAttributes
.getErrorAttributes(buildServerRequest(request, error), false);
assertThat(attributes.get("error"))
.isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
}
@Test
public void annotatedResponseStatusCodeWithCustomReasonPhrase() {
Exception error = new Custom2Exception();
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Map<String, Object> attributes = this.errorAttributes
.getErrorAttributes(buildServerRequest(request, error), false);
assertThat(attributes.get("error"))
.isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
assertThat(attributes.get("message")).isEqualTo("Nope!");
}
@Test @Test
public void includeStatusCode() { public void includeStatusCode() {
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
@ -214,4 +238,14 @@ public class DefaultErrorAttributesTests {
return 42; return 42;
} }
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
private static class CustomException extends RuntimeException {
}
@ResponseStatus(value = HttpStatus.I_AM_A_TEAPOT, reason = "Nope!")
private static class Custom2Exception extends RuntimeException {
}
} }

Loading…
Cancel
Save