Skip to content

Commit 7dc1847

Browse files
youngzilnobodyiam
andauthored
refactor(apollo-common): 异常处理增加根本原因信息 (#5367)
* refactor(apollo-common): 异常处理增加根本原因信息 * refactor(apollo-common): 优化异常处理信息展示 * Update CHANGES.md --------- Co-authored-by: Jason Song <[email protected]>
1 parent 7380bde commit 7dc1847

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Diff for: CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Apollo 2.5.0
99
* [Feature: Provide a new configfiles API to return the raw content of configuration files directly](https://github.com/apolloconfig/apollo/pull/5336)
1010
* [Feature: Enhanced instance configuration auditing and caching](https://github.com/apolloconfig/apollo/pull/5361)
1111
* [Feature: Provide a new open API to return the organization list](https://github.com/apolloconfig/apollo/pull/5365)
12+
* [Refactor: Exception handler adds root cause information](https://github.com/apolloconfig/apollo/pull/5367)
1213

1314
------------------
1415
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/16?closed=1)

Diff for: apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.slf4j.Logger;
3434
import org.slf4j.LoggerFactory;
3535
import org.slf4j.event.Level;
36+
import org.springframework.core.NestedExceptionUtils;
3637
import org.springframework.http.HttpHeaders;
3738
import org.springframework.http.HttpStatus;
3839
import org.springframework.http.MediaType;
@@ -53,6 +54,7 @@
5354

5455
@ControllerAdvice
5556
public class GlobalDefaultExceptionHandler {
57+
5658
private Gson gson = new Gson();
5759
private static Type mapType = new TypeToken<Map<String, Object>>() {
5860
}.getType();
@@ -115,7 +117,7 @@ private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest reque
115117

116118
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
117119
HttpStatus status, Throwable ex, Level logLevel) {
118-
String message = ex.getMessage();
120+
String message = getMessageWithRootCause(ex);
119121
printLog(message, ex, logLevel);
120122

121123
Map<String, Object> errorAttributes = new HashMap<>();
@@ -169,4 +171,13 @@ private void printLog(String message, Throwable ex, Level logLevel) {
169171
Tracer.logError(ex);
170172
}
171173

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+
172183
}

0 commit comments

Comments
 (0)