Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Feb 4, 2020
1 parent aead3a7 commit 12b644d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,37 @@ public Map<String, Object> getErrorAttributes(ServerRequest request, boolean inc
errorAttributes.put("timestamp", new Date());
errorAttributes.put("path", request.path());
Throwable error = getError(request);
HttpStatus errorStatus = determineHttpStatus(error);
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(),
ResponseStatus.class);
HttpStatus errorStatus = determineHttpStatus(error, responseStatus);
errorAttributes.put("status", errorStatus.value());
errorAttributes.put("error", errorStatus.getReasonPhrase());
errorAttributes.put("message", determineMessage(error));
errorAttributes.put("message", determineMessage(error, responseStatus));
handleException(errorAttributes, determineException(error), includeStackTrace);
return errorAttributes;
}

private HttpStatus determineHttpStatus(Throwable error) {
private HttpStatus determineHttpStatus(Throwable error, ResponseStatus responseStatus) {
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) {
private String determineMessage(Throwable error, ResponseStatus responseStatus) {
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) {
if (responseStatus != null && StringUtils.hasText(responseStatus.reason())) {
return responseStatus.reason();
}
return error.getMessage();
return (error.getMessage() != null) ? error.getMessage() : "";
}

private Throwable determineException(Throwable error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void annotatedResponseStatusCode() {
}

@Test
void annotatedResponseStatusCodeWithExceptionMessage() {
public void annotatedResponseStatusCodeWithExceptionMessage() {
Exception error = new CustomException("Test Message");
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error),
Expand Down

0 comments on commit 12b644d

Please sign in to comment.