Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ResponseStatusException;
Expand All @@ -46,6 +48,7 @@
*
* @author Brian Clozel
* @author Stephane Nicoll
* @author Michele Mancioppi
* @since 2.0.0
* @see ErrorAttributes
*/
Expand Down Expand Up @@ -91,16 +94,33 @@ private HttpStatus determineHttpStatus(Throwable error) {
if (error instanceof ResponseStatusException) {
return ((ResponseStatusException) error).getStatus();
}

ResponseStatus responseStatus = AnnotatedElementUtils
.findMergedAnnotation(error.getClass(), ResponseStatus.class);

if (responseStatus != null) {
return responseStatus.code();
}

return HttpStatus.INTERNAL_SERVER_ERROR;
}

private String determineMessage(Throwable error) {
if (error instanceof WebExchangeBindException) {
return error.getMessage();
}

if (error instanceof ResponseStatusException) {
return ((ResponseStatusException) error).getReason();
}

ResponseStatus responseStatus = AnnotatedElementUtils
.findMergedAnnotation(error.getClass(), ResponseStatus.class);

if (responseStatus != null) {
return responseStatus.reason();
}

return error.getMessage();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.validation.BindingResult;
import org.springframework.validation.MapBindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ResponseStatusException;
Expand Down Expand Up @@ -87,6 +88,29 @@ public void defaultStatusCode() {
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
public void includeStatusCode() {
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Expand Down Expand Up @@ -211,4 +235,14 @@ public int method(String firstParam) {
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 {

}

}