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

feat(auth): throw extended AuthError for service errors from user pool & id pool #14230

Conversation

AllanZhengYP
Copy link
Member

@AllanZhengYP AllanZhengYP commented Feb 20, 2025

Description of changes

  • Add an optional metadata property to the base AmplifyError class. So customers can inspect the requestId if an AuthError is caused by a service error response.
  • Fix the 2 identity pool APIs(GetId, GetCredentialsForId) to throw AuthError. Previously, the 2 APIs throw the base Error instance.
  • Also pass the internal $metadata from custom clients to metadata of the AmplifyError/AuthError.
  • Update the unit test for identity pool and user pool APIs to verify they all throw AuthError.

Issue #, if available

Description of how you validated changes

Unit tests

Checklist

  • PR description included
  • yarn test passes
  • Unit Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

Checklist for repo maintainers

  • Verify E2E tests for existing workflows are working as expected or add E2E tests for newly added workflows
  • New source file paths included in this PR have been added to CODEOWNERS, if appropriate

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@AllanZhengYP AllanZhengYP requested review from cshfang, HuiSF, ashika112 and a team as code owners February 20, 2025 19:26
Copy link
Member Author

Choose a reason for hiding this comment

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

The diff is messy but I only added the 1st test case.

...(statusCode === 404
? {
recoverySuggestion:
'Please add the object with this key to the bucket as the key is not found.',
Copy link
Member

Choose a reason for hiding this comment

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

nit: What's the error.message for 404 error? Do we actually need to provide a recovery suggestion for 404 errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

We actually to need it because the Storage.getProperties 's HEAD request does not return a payload. So in that case we won't have any message other than recoverySuggestion.

Copy link
Member Author

Choose a reason for hiding this comment

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

But since we already have special handling of the error name for 404(setting name to NotFound), this is indeed unnecessary. I will remove it in the next PR where I put all the changes to the Storage category

}: AmplifyErrorParams) {
super(message);

this.name = name;
this.underlyingError = underlyingError;
this.recoverySuggestion = recoverySuggestion;
if (underlyingError) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: why do we need this extra guard, underlyingError and recoverySuggestion are supposed to be optional (undefined is allowed above 😅)

Copy link
Member Author

Choose a reason for hiding this comment

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

@HuiSF It's very minor difference. If we always set these properties, we might as well just model these properties like readonly recoverySuggestion: string | undefined;

Comment on lines 37 to 45
this.metadata = {
...(metadata.extendedRequestId
? { extendedRequestId: metadata.extendedRequestId }
: {}),
...(metadata.httpStatusCode
? { httpStatusCode: metadata.httpStatusCode }
: {}),
...(metadata.requestId ? { requestId: metadata.requestId } : {}),
};
Copy link
Member

Choose a reason for hiding this comment

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

nit: can be simplified as the following, as ResponseMetadata allows there properties to contain undefined as values?

const { extendedRequestId, httpStatusCode, httpStatusCode } = metadata;
this.metadata = {
  extendedRequestId,
  httpStatusCode,
  httpStatusCode,
};

@AllanZhengYP AllanZhengYP requested a review from a team as a code owner February 21, 2025 01:47
@AllanZhengYP AllanZhengYP force-pushed the feat/monitoring-instrument/main branch from e69f680 to eb08d00 Compare February 21, 2025 01:53
@AllanZhengYP
Copy link
Member Author

I have to bump up the bundle size limit in a lot of packages for a little, because the AmplifyError increases couple tens of bytes.

@AllanZhengYP AllanZhengYP requested a review from HuiSF February 21, 2025 02:10
Copy link
Member

@ashika112 ashika112 left a comment

Choose a reason for hiding this comment

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

LGTM

expect(deserializer(response as any)).rejects.toThrow(
new AuthError({
try {
await deserializer(response as any);
Copy link
Member

Choose a reason for hiding this comment

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

Can we use expect().rejects.toThrow() instead to ensure the assertions in the catch block actually happen?

Copy link
Member Author

@AllanZhengYP AllanZhengYP Feb 21, 2025

Choose a reason for hiding this comment

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

toThrow() does not verify the non-message part of the error instance. See line 37, I made sure the assertion actually runs with expect.assertions()

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense 👍

@AllanZhengYP AllanZhengYP merged commit 7af353b into aws-amplify:feat/monitoring-instrument/main Feb 21, 2025
28 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.

3 participants