Skip to content

Commit ce9c7f0

Browse files
authored
improv(parameters): include cause in set parameter error (#3047)
1 parent 8fd5479 commit ce9c7f0

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

packages/parameters/src/errors.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/**
22
* Error thrown when a parameter cannot be retrieved.
3+
*
4+
* You can use this error to catch and handle errors when getting a parameter, the `cause` property will contain the original error.
35
*/
46
class GetParameterError extends Error {
5-
public constructor(message?: string) {
6-
super(message);
7+
public constructor(message?: string, options?: ErrorOptions) {
8+
super(message, options);
79
this.name = 'GetParameterError';
810
}
911
}
1012

1113
/**
1214
* Error thrown when a parameter cannot be set.
15+
*
16+
* You can use this error to catch and handle errors when setting a parameter, the `cause` property will contain the original error.
1317
*/
1418
class SetParameterError extends Error {
15-
public constructor(message?: string) {
16-
super(message);
19+
public constructor(message?: string, options?: ErrorOptions) {
20+
super(message, options);
1721
this.name = 'SetParameterError';
1822
}
1923
}

packages/parameters/src/ssm/SSMProvider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,12 @@ class SSMProvider extends BaseProvider {
376376
try {
377377
result = await this.client.send(new PutParameterCommand(sdkOptions));
378378
} catch (error) {
379-
throw new SetParameterError(`Unable to set parameter with name ${name}`);
379+
throw new SetParameterError(`Unable to set parameter with name ${name}`, {
380+
cause: error,
381+
});
380382
}
381383

382-
// biome-ignore lint/style/noNonNullAssertion: The API for PutParameter states that there will always be a value returned when the request was successful.
383-
return result.Version!;
384+
return result.Version as number;
384385
}
385386

386387
/**

packages/parameters/src/ssm/setParameter.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,21 @@ import type { SSMSetOptions } from '../types/SSMProvider.js';
33
import { SSMProvider } from './SSMProvider.js';
44

55
/**
6-
* ## Intro
7-
* The Parameters utility provides an SSMProvider that allows setting parameters in AWS Systems Manager.
6+
* Set a parameter in AWS Systems Manager Parameter Store.
87
*
9-
* ## Getting started
10-
*
11-
* This utility supports AWS SDK v3 for JavaScript only. This allows the utility to be modular, and you to install only
12-
* the SDK packages you need and keep your bundle size small.
13-
*
14-
* To use the provider, you must install the Parameters utility and the AWS SDK v3 for JavaScript for SSM:
15-
*
16-
* ```sh
17-
* npm install @aws-lambda-powertools/parameters @aws-sdk/client-ssm
18-
*```
19-
*
20-
* ## Basic Usage
8+
* **Basic Usage**
219
*
2210
* @example
2311
* ```typescript
2412
* import { setParameter } from '@aws-lambda-powertools/parameters/ssm';
2513
*
26-
* export const handler = async (): Promise<void> => {
27-
* // Set a parameter
28-
* const version = await setParameter('/my-parameter', { value: 'my-value' });
29-
* console.log(Parameter version: ${version});
14+
* export const handler = async () => {
15+
* // Set a parameter
16+
* const version = await setParameter('/my-parameter', { value: 'my-value' });
3017
* };
3118
* ```
3219
*
33-
* ## Advanced Usage
34-
*
35-
* ### Overwriting a parameter
20+
* **Overwriting a parameter**
3621
*
3722
* By default, the provider will not overwrite a parameter if it already exists. You can force the provider to overwrite the parameter by using the `overwrite` option.
3823
*
@@ -50,7 +35,7 @@ import { SSMProvider } from './SSMProvider.js';
5035
* };
5136
* ```
5237
*
53-
* ### Extra SDK options
38+
* **Extra SDK options**
5439
*
5540
* When setting a parameter, you can pass extra options to the AWS SDK v3 for JavaScript client by using the sdkOptions parameter.
5641
*
@@ -67,17 +52,15 @@ import { SSMProvider } from './SSMProvider.js';
6752
* },
6853
* });
6954
* };
70-
* ```
71-
*
72-
* This object accepts the same options as the AWS SDK v3 for JavaScript `PutParameterCommandInput` interface.
55+
* ```
7356
*
74-
* ### Built-in provider class
57+
* This object accepts the same options as the AWS SDK v3 for JavaScript `PutParameterCommandInput` interface.
7558
*
76-
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link SSMProvider} class.
59+
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link SSMProvider} utility.
7760
*
78-
* ### Options
61+
* **Options**
7962
*
80-
* You can customize the storage of the value by passing options to the function:
63+
* You can customize the storage of the value by passing options to the function:
8164
* * `value` - The value of the parameter, which is a mandatory option.
8265
* * `overwrite` - Whether to overwrite the value if it already exists (default: `false`)
8366
* * `description` - The description of the parameter
@@ -88,8 +71,8 @@ import { SSMProvider } from './SSMProvider.js';
8871
*
8972
* For more usage examples, see [our documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/).
9073
*
91-
* @param {string} name - Name of the parameter
92-
* @param {SSMSetOptions} options - Options to configure the parameter
74+
* @param name - Name of the parameter
75+
* @param options - Options to configure the parameter
9376
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/
9477
*/
9578
const setParameter = async <

0 commit comments

Comments
 (0)