Skip to content

refactor(apollo-common): 异常处理增加根本原因信息#5367

Merged
nobodyiam merged 4 commits into
apolloconfig:masterfrom
youngzil:feature/add-root-cause-info-to-exceptionhander
Apr 1, 2025
Merged

refactor(apollo-common): 异常处理增加根本原因信息#5367
nobodyiam merged 4 commits into
apolloconfig:masterfrom
youngzil:feature/add-root-cause-info-to-exceptionhander

Conversation

@youngzil

@youngzil youngzil commented Mar 26, 2025

Copy link
Copy Markdown
Contributor

What's the purpose of this PR

XXXXX

Which issue(s) this PR fixes:

Fixes #5366

Brief changelog

XXXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Read the Contributing Guide before making this pull request.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit tests to verify the code.
  • Run mvn clean test to make sure this pull request doesn't break anything.
  • Update the CHANGES log.

Summary by CodeRabbit

  • New Features
    • Enhanced error handling now provides more descriptive error messages by including additional details on the underlying issues.
  • Documentation
    • Added a new entry in the release notes for Apollo version 2.5.0, highlighting the refactor related to exception handling.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 26, 2025
@coderabbitai

coderabbitai Bot commented Mar 26, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes add a private method getMessageWithRootCause(Throwable ex) to the GlobalDefaultExceptionHandler class. This method retrieves the root cause of an exception using NestedExceptionUtils.getMostSpecificCause(ex) and appends its message to the original exception message if they differ. The handleError method has been updated to use this new method instead of directly calling ex.getMessage(). An import for NestedExceptionUtils has been added to support the new functionality.

Changes

File Change Summary
apollo-common/src/.../GlobalDefaultExceptionHandler.java Added private method getMessageWithRootCause(Throwable ex) to extract the root cause message using NestedExceptionUtils; updated handleError to use the new method; added import for NestedExceptionUtils.
CHANGES.md Added entry for Apollo version 2.5.0 highlighting the refactor related to exception handling, specifically the inclusion of root cause information.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Handler as GlobalDefaultExceptionHandler
    participant Utils as NestedExceptionUtils
    Client->>Handler: Trigger error handling (handleError)
    Handler->>Handler: Call getMessageWithRootCause(ex)
    Handler->>Utils: getMostSpecificCause(ex)
    Utils-->>Handler: Return root cause
    Handler->>Handler: Append root cause message if different
    Handler-->>Client: Return detailed error response
Loading

Assessment against linked issues

Objective Addressed Explanation
Enhance error messages to include detailed root cause information (#5366)

Suggested labels

lgtm

Poem

I'm a little rabbit hopping through the code,
Uncovering roots on each mysterious road.
With a twitch of my whiskers, bugs fade away,
As detailed messages now lead the way.
Hop on, code world, and brighten the day! 🐰💻


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 88415b3 and 49a13d6.

📒 Files selected for processing (1)
  • CHANGES.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGES.md
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build (8)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java (1)

174-181: Consider adding null handling for exception messages.

The implementation correctly identifies and appends root cause messages, enhancing error traceability. However, if either ex.getMessage() or rootCause.getMessage() returns null, the error message will contain the string "null" which isn't ideal for logs.

private String getMessageWithRootCause(Throwable ex) {
-  String message = ex.getMessage();
+  String message = ex.getMessage() != null ? ex.getMessage() : "No message";
  Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(ex);
  if (rootCause != ex) {
-    message += "; rootCause message " + rootCause.getMessage();
+    String rootCauseMessage = rootCause.getMessage() != null ? 
+        rootCause.getMessage() : "No message";
+    message += "; rootCause message " + rootCauseMessage;
  }
  return message;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 09e02fb and dfd539e.

📒 Files selected for processing (1)
  • apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build (8)
🔇 Additional comments (2)
apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java (2)

36-36: Import added for root cause extraction.

The addition of NestedExceptionUtils import is appropriate for the new functionality that extracts root causes from exceptions.


120-120: Enhanced error message with root cause information.

Good improvement to replace ex.getMessage() with getMessageWithRootCause(ex) to provide more comprehensive error information, making debugging easier.

Comment on lines +174 to +181
private String getMessageWithRootCause(Throwable ex) {
String message = ex.getMessage();
Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(ex);
if (rootCause != ex) {
message += "; rootCause message " + rootCause.getMessage();
}
return message;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

❓ Verification inconclusive

Consider 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 getMessageWithRootCause in apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java (lines 174–181) now enhances exception handling by appending the root cause message. However, our investigation did not reveal any unit tests covering this functionality. To ensure robustness, please add tests addressing scenarios such as:

  • Nested Exceptions: Verify that when an exception has a distinct root cause, its message is correctly appended.
  • Null Messages: Confirm that the method handles cases where either the primary exception or its root cause has a null message.
  • Identical Exceptions: Ensure that there’s proper handling when the root cause is the same as the original exception.

These tests will help maintain reliable error handling across various failure modes.

Comment thread CHANGES.md Outdated

@nobodyiam nobodyiam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 1, 2025
@nobodyiam
nobodyiam merged commit 7dc1847 into apolloconfig:master Apr 1, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 1, 2025
@youngzil
youngzil deleted the feature/add-root-cause-info-to-exceptionhander branch April 2, 2025 02:19
@nobodyiam nobodyiam added this to the 2.5.0 milestone Feb 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GlobalDefaultExceptionHandler异常message缺失具体异常信息

2 participants