Skip to content

improv(parameters): include cause in set parameter error #3047

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

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/parameters/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
/**
* Error thrown when a parameter cannot be retrieved.
*
* You can use this error to catch and handle errors when getting a parameter, the `cause` property will contain the original error.
*/
class GetParameterError extends Error {
public constructor(message?: string) {
super(message);
public constructor(message?: string, options?: ErrorOptions) {
super(message, options);
this.name = 'GetParameterError';
}
}

/**
* Error thrown when a parameter cannot be set.
*
* You can use this error to catch and handle errors when setting a parameter, the `cause` property will contain the original error.
*/
class SetParameterError extends Error {
public constructor(message?: string) {
super(message);
public constructor(message?: string, options?: ErrorOptions) {
super(message, options);
this.name = 'SetParameterError';
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/parameters/src/ssm/SSMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,12 @@ class SSMProvider extends BaseProvider {
try {
result = await this.client.send(new PutParameterCommand(sdkOptions));
} catch (error) {
throw new SetParameterError(`Unable to set parameter with name ${name}`);
throw new SetParameterError(`Unable to set parameter with name ${name}`, {
cause: error,
});
}

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

/**
Expand Down
45 changes: 14 additions & 31 deletions packages/parameters/src/ssm/setParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,21 @@ import type { SSMSetOptions } from '../types/SSMProvider.js';
import { SSMProvider } from './SSMProvider.js';

/**
* ## Intro
* The Parameters utility provides an SSMProvider that allows setting parameters in AWS Systems Manager.
* Set a parameter in AWS Systems Manager Parameter Store.
*
* ## Getting started
*
* This utility supports AWS SDK v3 for JavaScript only. This allows the utility to be modular, and you to install only
* the SDK packages you need and keep your bundle size small.
*
* To use the provider, you must install the Parameters utility and the AWS SDK v3 for JavaScript for SSM:
*
* ```sh
* npm install @aws-lambda-powertools/parameters @aws-sdk/client-ssm
*```
*
* ## Basic Usage
* **Basic Usage**
*
* @example
* ```typescript
* import { setParameter } from '@aws-lambda-powertools/parameters/ssm';
*
* export const handler = async (): Promise<void> => {
* // Set a parameter
* const version = await setParameter('/my-parameter', { value: 'my-value' });
* console.log(Parameter version: ${version});
* export const handler = async () => {
* // Set a parameter
* const version = await setParameter('/my-parameter', { value: 'my-value' });
* };
* ```
*
* ## Advanced Usage
*
* ### Overwriting a parameter
* **Overwriting a parameter**
*
* 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.
*
Expand All @@ -50,7 +35,7 @@ import { SSMProvider } from './SSMProvider.js';
* };
* ```
*
* ### Extra SDK options
* **Extra SDK options**
*
* When setting a parameter, you can pass extra options to the AWS SDK v3 for JavaScript client by using the sdkOptions parameter.
*
Expand All @@ -67,17 +52,15 @@ import { SSMProvider } from './SSMProvider.js';
* },
* });
* };
* ```
*
* This object accepts the same options as the AWS SDK v3 for JavaScript `PutParameterCommandInput` interface.
* ```
*
* ### Built-in provider class
* This object accepts the same options as the AWS SDK v3 for JavaScript `PutParameterCommandInput` interface.
*
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link SSMProvider} class.
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link SSMProvider} utility.
*
* ### Options
* **Options**
*
* You can customize the storage of the value by passing options to the function:
* You can customize the storage of the value by passing options to the function:
* * `value` - The value of the parameter, which is a mandatory option.
* * `overwrite` - Whether to overwrite the value if it already exists (default: `false`)
* * `description` - The description of the parameter
Expand All @@ -88,8 +71,8 @@ import { SSMProvider } from './SSMProvider.js';
*
* For more usage examples, see [our documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/).
*
* @param {string} name - Name of the parameter
* @param {SSMSetOptions} options - Options to configure the parameter
* @param name - Name of the parameter
* @param options - Options to configure the parameter
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/
*/
const setParameter = async <
Expand Down