Skip to content

Commit

Permalink
Handle parameter not found error (#2030)
Browse files Browse the repository at this point in the history
* Handle parameter not found error

* Handle parameter not found error
  • Loading branch information
sobolk committed Sep 19, 2024
1 parent 603b75d commit dce0518
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-bulldogs-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-secret': patch
---

Handle parameter not found error
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ void describe('getSecretClientWithAmplifyErrorHandling', () => {
);
});

void it('throws AmplifyUserError if getSecret fails due to ParameterNotFound error', async (context) => {
const notFoundError = new Error('Parameter not found error');
notFoundError.name = 'ParameterNotFound';
const secretsError = SecretError.createInstance(notFoundError);
context.mock.method(rawSecretClient, 'getSecret', () => {
throw secretsError;
});
const secretName = 'testSecretName';
await assert.rejects(
() =>
classUnderTest.getSecret(
{
namespace: 'testSandboxId',
name: 'testSandboxName',
type: 'sandbox',
},
{
name: secretName,
}
),
new AmplifyUserError('SSMParameterNotFoundError', {
message: `Failed to get ${secretName} secret. ParameterNotFound: Parameter not found error`,
resolution: `Make sure that ${secretName} has been set. See https://docs.amplify.aws/react/deploy-and-host/fullstack-branching/secrets-and-vars/.`,
})
);
});

void it('throws AmplifyFault if listSecrets fails due to a non-SSM exception other than expired credentials', async (context) => {
const underlyingError = new Error('some secret error');
const secretsError = SecretError.createInstance(underlyingError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class SSMSecretClientWithAmplifyErrorHandling implements SecretClient {
secretIdentifier
);
} catch (e) {
throw this.translateToAmplifyError(e, 'Get');
throw this.translateToAmplifyError(e, 'Get', secretIdentifier);
}
};

Expand Down Expand Up @@ -73,7 +73,11 @@ export class SSMSecretClientWithAmplifyErrorHandling implements SecretClient {
}
};

private translateToAmplifyError = (error: unknown, apiName: string) => {
private translateToAmplifyError = (
error: unknown,
apiName: string,
secretIdentifier?: SecretIdentifier
) => {
if (error instanceof SecretError && error.cause) {
if (
[
Expand All @@ -94,6 +98,16 @@ export class SSMSecretClientWithAmplifyErrorHandling implements SecretClient {
'Make sure your AWS credentials are set up correctly, refreshed and have necessary permissions to call SSM service',
});
}
if (
error.cause.name === 'ParameterNotFound' &&
apiName === 'Get' &&
secretIdentifier
) {
return new AmplifyUserError('SSMParameterNotFoundError', {
message: `Failed to get ${secretIdentifier.name} secret. ${error.cause.name}: ${error.cause?.message}`,
resolution: `Make sure that ${secretIdentifier.name} has been set. See https://docs.amplify.aws/react/deploy-and-host/fullstack-branching/secrets-and-vars/.`,
});
}
let downstreamException: Error = error;
if (
!(error.cause instanceof SSMServiceException) &&
Expand Down

0 comments on commit dce0518

Please sign in to comment.