Skip to content

Commit dce0518

Browse files
author
Kamil Sobol
authored
Handle parameter not found error (#2030)
* Handle parameter not found error * Handle parameter not found error
1 parent 603b75d commit dce0518

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

.changeset/silver-bulldogs-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-secret': patch
3+
---
4+
5+
Handle parameter not found error

packages/backend-secret/src/ssm_secret_with_amplify_error_handling.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ void describe('getSecretClientWithAmplifyErrorHandling', () => {
6262
);
6363
});
6464

65+
void it('throws AmplifyUserError if getSecret fails due to ParameterNotFound error', async (context) => {
66+
const notFoundError = new Error('Parameter not found error');
67+
notFoundError.name = 'ParameterNotFound';
68+
const secretsError = SecretError.createInstance(notFoundError);
69+
context.mock.method(rawSecretClient, 'getSecret', () => {
70+
throw secretsError;
71+
});
72+
const secretName = 'testSecretName';
73+
await assert.rejects(
74+
() =>
75+
classUnderTest.getSecret(
76+
{
77+
namespace: 'testSandboxId',
78+
name: 'testSandboxName',
79+
type: 'sandbox',
80+
},
81+
{
82+
name: secretName,
83+
}
84+
),
85+
new AmplifyUserError('SSMParameterNotFoundError', {
86+
message: `Failed to get ${secretName} secret. ParameterNotFound: Parameter not found error`,
87+
resolution: `Make sure that ${secretName} has been set. See https://docs.amplify.aws/react/deploy-and-host/fullstack-branching/secrets-and-vars/.`,
88+
})
89+
);
90+
});
91+
6592
void it('throws AmplifyFault if listSecrets fails due to a non-SSM exception other than expired credentials', async (context) => {
6693
const underlyingError = new Error('some secret error');
6794
const secretsError = SecretError.createInstance(underlyingError);

packages/backend-secret/src/ssm_secret_with_amplify_error_handling.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class SSMSecretClientWithAmplifyErrorHandling implements SecretClient {
2929
secretIdentifier
3030
);
3131
} catch (e) {
32-
throw this.translateToAmplifyError(e, 'Get');
32+
throw this.translateToAmplifyError(e, 'Get', secretIdentifier);
3333
}
3434
};
3535

@@ -73,7 +73,11 @@ export class SSMSecretClientWithAmplifyErrorHandling implements SecretClient {
7373
}
7474
};
7575

76-
private translateToAmplifyError = (error: unknown, apiName: string) => {
76+
private translateToAmplifyError = (
77+
error: unknown,
78+
apiName: string,
79+
secretIdentifier?: SecretIdentifier
80+
) => {
7781
if (error instanceof SecretError && error.cause) {
7882
if (
7983
[
@@ -94,6 +98,16 @@ export class SSMSecretClientWithAmplifyErrorHandling implements SecretClient {
9498
'Make sure your AWS credentials are set up correctly, refreshed and have necessary permissions to call SSM service',
9599
});
96100
}
101+
if (
102+
error.cause.name === 'ParameterNotFound' &&
103+
apiName === 'Get' &&
104+
secretIdentifier
105+
) {
106+
return new AmplifyUserError('SSMParameterNotFoundError', {
107+
message: `Failed to get ${secretIdentifier.name} secret. ${error.cause.name}: ${error.cause?.message}`,
108+
resolution: `Make sure that ${secretIdentifier.name} has been set. See https://docs.amplify.aws/react/deploy-and-host/fullstack-branching/secrets-and-vars/.`,
109+
});
110+
}
97111
let downstreamException: Error = error;
98112
if (
99113
!(error.cause instanceof SSMServiceException) &&

0 commit comments

Comments
 (0)