-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Exception is thrown when component failed to register #1374
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
Conversation
WalkthroughThe change updates the exception handling in the Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant ComponentManagerImpl
participant ComponentInfo
Caller->>ComponentManagerImpl: doRegister(ci)
ComponentManagerImpl->>ComponentInfo: register()
alt register() succeeds
ComponentManagerImpl->>ComponentManagerImpl: insert registry, resolve, activate
alt all succeed
ComponentManagerImpl-->>Caller: return ComponentInfo
else exception during registry/resolve/activate
ComponentManagerImpl->>ComponentManagerImpl: log error and call ci.exception()
ComponentManagerImpl-->>Caller: throw ServiceRuntimeException
end
else register() throws Exception
ComponentManagerImpl->>ComponentManagerImpl: log error
ComponentManagerImpl-->>Caller: throw ServiceRuntimeException
end
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms (3)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🔭 Outside diff range comments (2)
sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java (2)
186-191
:⚠️ Potential issueInclude the original exception as the cause in ServiceRuntimeException.
The exception handling correctly propagates the registration failure, but the original exception
t
is not included as the cause. This results in loss of valuable debugging information like the stack trace.Apply this diff to preserve the original exception:
} catch (Throwable t) { LOGGER.error(ErrorCode.convert("01-03003", ci.getName()), t); - throw new ServiceRuntimeException(ErrorCode.convert("01-03003", ci.getName())); + throw new ServiceRuntimeException(ErrorCode.convert("01-03003", ci.getName()), t); }
176-191
: 💡 Verification agent🧩 Analysis chain
Verify callers can handle the new exception behavior.
This change modifies the behavior from silent failure to throwing exceptions. While this improves error propagation, it's a breaking change that could affect existing callers.
Run the following script to identify all callers of the affected methods:
Additionally, consider updating the method's JavaDoc to document this exception behavior.
🏁 Script executed:
#!/bin/bash # Description: Find all callers of register() and registerAndGet() methods to verify they can handle exceptions # Search for direct calls to register method echo "=== Direct calls to register() ===" rg -A 3 '\.register\(' --type java # Search for direct calls to registerAndGet method echo -e "\n=== Direct calls to registerAndGet() ===" rg -A 3 '\.registerAndGet\(' --type java # Search for ComponentManager interface usage echo -e "\n=== ComponentManager interface usage ===" ast-grep --pattern 'ComponentManager $_ = $_'Length of output: 37810
Document new exception behavior and review callers
Even though
ServiceRuntimeException
is unchecked, this change can break existing integrations by propagating errors that were previously swallowed. Please:• Update the JavaDoc for both
ComponentManager.register(...)
andComponentManager.registerAndGet(...)
(and their implementations) to declare
@throws ServiceRuntimeException
and describe the conditions under which it’s thrown.
• Audit any high-level callers (e.g., Spring factories, service/client initializers) to decide whether they should
- Let the exception bubble up (fail fast), or
- Catch it and handle it gracefully.
Tags:
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: build (21)
- GitHub Check: build (17)
- GitHub Check: sca
} catch (Throwable t) { | ||
ci.exception(new Exception(t)); | ||
LOGGER.error(ErrorCode.convert("01-03004", ci.getName()), t); | ||
throw new ServiceRuntimeException(ErrorCode.convert("01-03004", ci.getName())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include the original exception as the cause in ServiceRuntimeException.
Similar to the previous exception handling, the original exception t
should be included as the cause to preserve debugging information.
Apply this diff to preserve the original exception:
} catch (Throwable t) {
ci.exception(new Exception(t));
LOGGER.error(ErrorCode.convert("01-03004", ci.getName()), t);
- throw new ServiceRuntimeException(ErrorCode.convert("01-03004", ci.getName()));
+ throw new ServiceRuntimeException(ErrorCode.convert("01-03004", ci.getName()), t);
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
} catch (Throwable t) { | |
ci.exception(new Exception(t)); | |
LOGGER.error(ErrorCode.convert("01-03004", ci.getName()), t); | |
throw new ServiceRuntimeException(ErrorCode.convert("01-03004", ci.getName())); | |
} | |
} catch (Throwable t) { | |
ci.exception(new Exception(t)); | |
LOGGER.error(ErrorCode.convert("01-03004", ci.getName()), t); | |
throw new ServiceRuntimeException( | |
ErrorCode.convert("01-03004", ci.getName()), | |
t | |
); | |
} |
🤖 Prompt for AI Agents
In
sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java
around lines 209 to 213, the catch block creates a new ServiceRuntimeException
without passing the original Throwable `t` as the cause, which can hinder
debugging. Fix this by passing `t` as the cause when constructing the
ServiceRuntimeException, ensuring the original exception details are preserved
in the stack trace.
ab1db35
to
9f24ccb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java
(2 hunks)sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/impl/ComponentManagerImplTests.java
(8 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: build (17)
- GitHub Check: build (21)
- GitHub Check: sca
🔇 Additional comments (3)
sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/impl/ComponentManagerImplTests.java (3)
50-50
: LGTM: OutputCaptureExtension added for test output validation.The addition of
OutputCaptureExtension.class
to the@ExtendWith
annotation is necessary for capturing console output in the updated test methods.
154-162
: Clarify test expectations in resolveException method.The test logic appears inconsistent. The assertion in the try block expects successful registration, but the catch block suggests a
ServiceRuntimeException
should be thrown. This creates ambiguity about what the test is actually validating.Please clarify the expected behavior:
- Should registration succeed but resolution fail and throw an exception?
- Or should both registration and resolution fail with an exception?
If registration should succeed but resolution should fail, consider this refactor:
try { assertThat(componentManager.registerAndGet(componentInfoA)).isEqualTo(componentInfoA); - + // The exception should be thrown during resolution, not registration + componentManager.resolvePendingResolveComponent(componentInfoA.getName()); + // Should not reach here if exception is thrown as expected + assertThat(false).as("Expected ServiceRuntimeException during resolution").isTrue(); } catch (ServiceRuntimeException e) { assertThat(componentInfoA.isHealthy().isHealthy()).isFalse(); assertThat(capturedOutput.getOut()).contains("01-03004"); assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString()); }
58-66
: LGTM: Formatting improvements enhance readability.The line continuation formatting improvements throughout the file enhance code readability without changing functionality.
Also applies to: 73-73, 87-91, 100-105, 168-172, 189-193, 210-214, 229-229, 240-242
try { | ||
assertThat(componentManager.registerAndGet(componentInfoA)).isNull(); | ||
} catch (ServiceRuntimeException e) { | ||
assertThat(capturedOutput.getOut()).contains("01-03003"); | ||
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix inconsistent test logic in registerException method.
The test logic is inconsistent. If a ServiceRuntimeException
is expected to be thrown (which it should be based on the PR objectives), then the assertion assertThat(componentManager.registerAndGet(componentInfoA)).isNull();
in the try block will never be executed and doesn't align with the exception-based behavior.
Apply this diff to fix the test logic:
try {
- assertThat(componentManager.registerAndGet(componentInfoA)).isNull();
+ componentManager.registerAndGet(componentInfoA);
+ // Should not reach here if exception is thrown as expected
+ assertThat(false).as("Expected ServiceRuntimeException to be thrown").isTrue();
} catch (ServiceRuntimeException e) {
assertThat(capturedOutput.getOut()).contains("01-03003");
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
try { | |
assertThat(componentManager.registerAndGet(componentInfoA)).isNull(); | |
} catch (ServiceRuntimeException e) { | |
assertThat(capturedOutput.getOut()).contains("01-03003"); | |
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString()); | |
} | |
} | |
try { | |
componentManager.registerAndGet(componentInfoA); | |
// Should not reach here if exception is thrown as expected | |
assertThat(false).as("Expected ServiceRuntimeException to be thrown").isTrue(); | |
} catch (ServiceRuntimeException e) { | |
assertThat(capturedOutput.getOut()).contains("01-03003"); | |
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString()); | |
} | |
} |
🤖 Prompt for AI Agents
In
sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/impl/ComponentManagerImplTests.java
around lines 141 to 147, the test incorrectly asserts a null return inside a try
block where a ServiceRuntimeException is expected, causing inconsistent logic.
Remove the assertion inside the try block and instead directly invoke the method
expecting the exception, then catch the ServiceRuntimeException and perform
assertions on the captured output to verify the error message and component
name.
Signed-off-by: JermaineHua <[email protected]>
9f24ccb
to
4bc2d92
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1374 +/- ##
============================================
- Coverage 82.91% 82.90% -0.02%
Complexity 2971 2971
============================================
Files 340 340
Lines 9828 9833 +5
Branches 1178 1178
============================================
+ Hits 8149 8152 +3
Misses 1164 1164
- Partials 515 517 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary by CodeRabbit