|
33 | 33 | import org.slf4j.Logger;
|
34 | 34 | import org.slf4j.LoggerFactory;
|
35 | 35 | import org.slf4j.event.Level;
|
| 36 | +import org.springframework.core.NestedExceptionUtils; |
36 | 37 | import org.springframework.http.HttpHeaders;
|
37 | 38 | import org.springframework.http.HttpStatus;
|
38 | 39 | import org.springframework.http.MediaType;
|
|
53 | 54 |
|
54 | 55 | @ControllerAdvice
|
55 | 56 | public class GlobalDefaultExceptionHandler {
|
| 57 | + |
56 | 58 | private Gson gson = new Gson();
|
57 | 59 | private static Type mapType = new TypeToken<Map<String, Object>>() {
|
58 | 60 | }.getType();
|
@@ -115,7 +117,7 @@ private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest reque
|
115 | 117 |
|
116 | 118 | private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
|
117 | 119 | HttpStatus status, Throwable ex, Level logLevel) {
|
118 |
| - String message = ex.getMessage(); |
| 120 | + String message = getMessageWithRootCause(ex); |
119 | 121 | printLog(message, ex, logLevel);
|
120 | 122 |
|
121 | 123 | Map<String, Object> errorAttributes = new HashMap<>();
|
@@ -169,4 +171,13 @@ private void printLog(String message, Throwable ex, Level logLevel) {
|
169 | 171 | Tracer.logError(ex);
|
170 | 172 | }
|
171 | 173 |
|
| 174 | + private String getMessageWithRootCause(Throwable ex) { |
| 175 | + String message = ex.getMessage(); |
| 176 | + Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(ex); |
| 177 | + if (rootCause != ex) { |
| 178 | + message += " [Cause: " + rootCause.getMessage() + "]"; |
| 179 | + } |
| 180 | + return message; |
| 181 | + } |
| 182 | + |
172 | 183 | }
|
0 commit comments