Skip to content
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

Create EC2MetadataClientException for IMDS 4XX errors #5947

Merged

Conversation

S-Saranya1
Copy link
Contributor

@S-Saranya1 S-Saranya1 commented Mar 10, 2025

Motivation and Context

The IMDS client currently throws SdkClientException for all 4XX errors, contradicting AWS SDK guidelines which state that client exceptions should only be used when the client fails to make a service call. When IMDS returns a response (even 4XX), it means the call was successful but rejected by the service. This improper error classification prevents specific error handling. Users can't implement specific handling for such cases because the client discards the HTTP status code information, forcing all errors to be treated the same way.
Fixing #5786

Modifications

  • Created a new Ec2MetadataClientException extending SdkClientException that preserves HTTP status codes from IMDS responses.
  • The exception includes better error messaging including response content and provides access to status codes via the statusCode() method.
  • Modified error handling in both sync and async implementations to throw this new exception for 4XX responses instead of the SdkClientException.

Testing

Added unit tests to verify the new Ec2MetadataClientException. Tests include mocked IMDS responses with different status codes (4XX), verifying status code preservation, error message formatting, and proper exception handling in both sync and async clients.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@S-Saranya1 S-Saranya1 requested a review from a team as a code owner March 10, 2025 23:30
@zoewangg
Copy link
Contributor

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
27.1% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

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

Change LGTM. Good job!

Let's have a mini APi surface area/support review with the team before merging

@S-Saranya1 S-Saranya1 changed the base branch from master to feature/master/imdsException March 20, 2025 20:52
@S-Saranya1 S-Saranya1 merged commit 3e0fd77 into feature/master/imdsException Mar 20, 2025
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants