-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
refactor(apollo-common): 异常处理增加根本原因信息 #5367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Map<String, Object>>() { | ||
| }.getType(); | ||
|
|
@@ -115,7 +117,7 @@ private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest reque | |
|
|
||
| private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request, | ||
| HttpStatus status, Throwable ex, Level logLevel) { | ||
| String message = ex.getMessage(); | ||
| String message = getMessageWithRootCause(ex); | ||
| printLog(message, ex, logLevel); | ||
|
|
||
| Map<String, Object> 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; | ||
| } | ||
|
Comment on lines
+174
to
+181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent ❓ Verification inconclusiveConsider adding unit tests for the new functionality. This method significantly improves exception handling by providing root cause information. To ensure this functionality works correctly under various scenarios (nested exceptions, null messages, etc.), please add comprehensive unit tests. 🏁 Script executed: #!/bin/bash
# Check if there are unit tests covering the new exception handling logic
# Try to find test files that might test the GlobalDefaultExceptionHandler
find . -path "*/test/*" -name "*GlobalDefaultExceptionHandler*Test.java" -o -name "*ExceptionHandler*Test.java"
# Look for test changes in the PR
git diff --name-only HEAD~1 HEAD | grep -E "Test\.java$" | xargs cat 2>/dev/null | grep -A 10 -B 10 "getMessageWithRootCause\|rootCause message"Length of output: 248 Action Required: Add Comprehensive Unit Tests for Exception Handling The method
These tests will help maintain reliable error handling across various failure modes. |
||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.