Skip to content

Commit

Permalink
Merge branch '2.1.x' into 2.2.x
Browse files Browse the repository at this point in the history
Closes gh-20028
  • Loading branch information
snicoll committed Feb 4, 2020
2 parents 90e3d88 + 29fb2a3 commit fa239a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down Expand Up @@ -108,7 +109,11 @@ private String determineMessage(Throwable error, MergedAnnotation<ResponseStatus
if (error instanceof ResponseStatusException) {
return ((ResponseStatusException) error).getReason();
}
return responseStatusAnnotation.getValue("reason", String.class).orElseGet(error::getMessage);
String reason = responseStatusAnnotation.getValue("reason", String.class).orElse("");
if (StringUtils.hasText(reason)) {
return reason;
}
return (error.getMessage() != null) ? error.getMessage() : "";
}

private Throwable determineException(Throwable error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,6 +89,18 @@ void annotatedResponseStatusCode() {
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("message")).isEqualTo("");
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
}

@Test
void annotatedResponseStatusCodeWithExceptionMessage() {
Exception error = new CustomException("Test Message");
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("message")).isEqualTo("Test Message");
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
}

Expand Down Expand Up @@ -226,6 +238,13 @@ int method(String firstParam) {
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
static class CustomException extends RuntimeException {

CustomException() {
}

CustomException(String message) {
super(message);
}

}

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

0 comments on commit fa239a0

Please sign in to comment.