diff --git a/CHANGES.md b/CHANGES.md index b5f8a78deca..2e88c8f4d03 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ Apollo 2.5.0 * [Feature: Provide a new configfiles API to return the raw content of configuration files directly](https://github.com/apolloconfig/apollo/pull/5336) * [Feature: Enhanced instance configuration auditing and caching](https://github.com/apolloconfig/apollo/pull/5361) * [Feature: Provide a new open API to return the organization list](https://github.com/apolloconfig/apollo/pull/5365) +* [Refactor: Exception handler adds root cause information](https://github.com/apolloconfig/apollo/pull/5367) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/16?closed=1) diff --git a/apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java b/apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java index 572c19ab7a2..17bee42ede0 100644 --- a/apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java +++ b/apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java @@ -33,6 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.event.Level; +import org.springframework.core.NestedExceptionUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -53,6 +54,7 @@ @ControllerAdvice public class GlobalDefaultExceptionHandler { + private Gson gson = new Gson(); private static Type mapType = new TypeToken>() { }.getType(); @@ -115,7 +117,7 @@ private ResponseEntity> handleError(HttpServletRequest reque private ResponseEntity> handleError(HttpServletRequest request, HttpStatus status, Throwable ex, Level logLevel) { - String message = ex.getMessage(); + String message = getMessageWithRootCause(ex); printLog(message, ex, logLevel); Map errorAttributes = new HashMap<>(); @@ -169,4 +171,13 @@ private void printLog(String message, Throwable ex, Level logLevel) { Tracer.logError(ex); } + private String getMessageWithRootCause(Throwable ex) { + String message = ex.getMessage(); + Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(ex); + if (rootCause != ex) { + message += " [Cause: " + rootCause.getMessage() + "]"; + } + return message; + } + }