|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 |
|
25 | 25 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
26 | 26 | import org.springframework.http.HttpStatus;
|
| 27 | +import org.springframework.util.StringUtils; |
27 | 28 | import org.springframework.validation.BindingResult;
|
28 | 29 | import org.springframework.validation.ObjectError;
|
29 | 30 | import org.springframework.web.bind.annotation.ResponseStatus;
|
@@ -80,39 +81,37 @@ public Map<String, Object> getErrorAttributes(ServerRequest request, boolean inc
|
80 | 81 | errorAttributes.put("timestamp", new Date());
|
81 | 82 | errorAttributes.put("path", request.path());
|
82 | 83 | Throwable error = getError(request);
|
83 |
| - HttpStatus errorStatus = determineHttpStatus(error); |
| 84 | + ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(), |
| 85 | + ResponseStatus.class); |
| 86 | + HttpStatus errorStatus = determineHttpStatus(error, responseStatus); |
84 | 87 | errorAttributes.put("status", errorStatus.value());
|
85 | 88 | errorAttributes.put("error", errorStatus.getReasonPhrase());
|
86 |
| - errorAttributes.put("message", determineMessage(error)); |
| 89 | + errorAttributes.put("message", determineMessage(error, responseStatus)); |
87 | 90 | handleException(errorAttributes, determineException(error), includeStackTrace);
|
88 | 91 | return errorAttributes;
|
89 | 92 | }
|
90 | 93 |
|
91 |
| - private HttpStatus determineHttpStatus(Throwable error) { |
| 94 | + private HttpStatus determineHttpStatus(Throwable error, ResponseStatus responseStatus) { |
92 | 95 | if (error instanceof ResponseStatusException) {
|
93 | 96 | return ((ResponseStatusException) error).getStatus();
|
94 | 97 | }
|
95 |
| - ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(), |
96 |
| - ResponseStatus.class); |
97 | 98 | if (responseStatus != null) {
|
98 | 99 | return responseStatus.code();
|
99 | 100 | }
|
100 | 101 | return HttpStatus.INTERNAL_SERVER_ERROR;
|
101 | 102 | }
|
102 | 103 |
|
103 |
| - private String determineMessage(Throwable error) { |
| 104 | + private String determineMessage(Throwable error, ResponseStatus responseStatus) { |
104 | 105 | if (error instanceof WebExchangeBindException) {
|
105 | 106 | return error.getMessage();
|
106 | 107 | }
|
107 | 108 | if (error instanceof ResponseStatusException) {
|
108 | 109 | return ((ResponseStatusException) error).getReason();
|
109 | 110 | }
|
110 |
| - ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(), |
111 |
| - ResponseStatus.class); |
112 |
| - if (responseStatus != null) { |
| 111 | + if (responseStatus != null && StringUtils.hasText(responseStatus.reason())) { |
113 | 112 | return responseStatus.reason();
|
114 | 113 | }
|
115 |
| - return error.getMessage(); |
| 114 | + return (error.getMessage() != null) ? error.getMessage() : ""; |
116 | 115 | }
|
117 | 116 |
|
118 | 117 | private Throwable determineException(Throwable error) {
|
|
0 commit comments