Skip to content

refactor: add IllegalArgumentException in getMostSpecificCause Method #34826

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sm9171
Copy link

@sm9171 sm9171 commented Apr 25, 2025

Add null check to NestedExceptionUtils.getMostSpecificCause

Description

This pull request adds a defensive null check to the getMostSpecificCause method in the NestedExceptionUtils class. The method now throws an IllegalArgumentException when a null value is passed as the original parameter.

Motivation

The getMostSpecificCause method is documented to never return null, as indicated by its JavaDoc comment: "return the most specific cause (never {@code null})". However, before this change, if a null value was passed to the method, it would attempt to call getRootCause(null) and then potentially return null, contradicting its contract.

Changes Made

Added a null check at the beginning of the getMostSpecificCause method that throws an IllegalArgumentException with the message "Original exception must not be null" when the original parameter is null.

Benefits

  1. Improved API Contract Enforcement: The method now properly enforces its documented contract of never returning null.
  2. Early Failure: Problems are detected at the source rather than propagating through the application, making debugging easier.
  3. Consistent Error Handling: This change aligns with Spring Framework's defensive programming practices seen throughout the codebase.
  4. Better Developer Experience: Provides a clear and immediate error message when the method is used incorrectly.

Impact

This is a non-breaking change for code that uses the method correctly (passing non-null exceptions). It only affects code that incorrectly passes null values, which would have led to unexpected behavior anyway.

The change is minimal and focused on improving robustness without altering the method's core functionality.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants