Skip to content
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
fix(bb-app-setting): throw ParameterNotFound for missing parameters o…
…n AWS
  • Loading branch information
xyos committed Oct 29, 2026
commit 3180892407b9dcbd7c1310fbb97373a62f79f06c
5 changes: 5 additions & 0 deletions .changeset/app-setting-aws-parameter-not-found.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-blocks/bb-app-setting': patch
---

`AppSetting.get()` on AWS now throws `AppSettingErrors.ParameterNotFound` when the SSM parameter does not exist, as documented, instead of the raw SDK `ParameterNotFound` exception.
37 changes: 34 additions & 3 deletions packages/bb-app-setting/src/index.aws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import assert from 'node:assert';
import { describe, test, mock } from 'node:test';
import { SSMClient } from '@aws-sdk/client-ssm';
import { Scope } from '@aws-blocks/core';
import { AppSetting } from './index.aws.js';
import { ParameterNotFound, SSMClient } from '@aws-sdk/client-ssm';
import { isBlocksError, isWireSafeError, Scope } from '@aws-blocks/core';
import { AppSetting, AppSettingErrors } from './index.aws.js';

const TEST_CMK = 'arn:aws:kms:us-east-1:111122223333:key/abcd1234-5678-90ab-cdef-1234567890ab';

Expand Down Expand Up @@ -57,3 +57,34 @@ describe('AWS runtime put() KMS key', () => {
}
});
});

describe('AWS runtime get() missing parameter', () => {
test('maps the SDK ParameterNotFound to a branded AppSettingErrors.ParameterNotFound', async () => {
const m = mock.method(SSMClient.prototype, 'send', async () => {
throw new ParameterNotFound({ message: 'not found', $metadata: {} });
});
try {
const setting = new AppSetting<string>(new Scope('app'), 'gone', { value: 'init' });
await assert.rejects(setting.get(), (e: unknown) => {
assert.ok(isBlocksError(e, AppSettingErrors.ParameterNotFound), `got ${(e as Error).name}`);
assert.ok(isWireSafeError(e));
return true;
});
} finally {
m.mock.restore();
}
});

test('rethrows other SDK errors unchanged', async () => {
const original = Object.assign(new Error('denied'), { name: 'AccessDeniedException' });
const m = mock.method(SSMClient.prototype, 'send', async () => {
throw original;
});
try {
const setting = new AppSetting<string>(new Scope('app'), 'denied', { value: 'init' });
await assert.rejects(setting.get(), (e: unknown) => e === original);
} finally {
m.mock.restore();
}
});
});
21 changes: 16 additions & 5 deletions packages/bb-app-setting/src/index.aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import {
SSMClient,
GetParameterCommand,
ParameterNotFound,
PutParameterCommand,
} from '@aws-sdk/client-ssm';
import type { GetParameterCommandOutput } from '@aws-sdk/client-ssm';
import { Scope, registerSdkIdentifiers, getSdkIdentifiers, installClientUserAgent, blocksError } from '@aws-blocks/core';
import type { ScopeParent } from '@aws-blocks/core';
import type { StandardSchemaV1 } from '@standard-schema/spec';
Expand Down Expand Up @@ -114,15 +116,24 @@ export class AppSetting<T = string> extends Scope {
* ```
*/
async get(): Promise<T> {
const result = await this.client.send(new GetParameterCommand({
Name: getSdkIdentifiers(this).parameterName,
WithDecryption: this.isSecret,
}));
const parameterName = getSdkIdentifiers(this).parameterName;
let result: GetParameterCommandOutput;
try {
result = await this.client.send(new GetParameterCommand({
Name: parameterName,
WithDecryption: this.isSecret,
}));
} catch (e) {
if (e instanceof ParameterNotFound || (e instanceof Error && e.name === 'ParameterNotFound')) {
throw blocksError(AppSettingErrors.ParameterNotFound, `Parameter "${parameterName}" does not exist`);
}
throw e;
}

this.client.config.customUserAgent = this.buildUserAgentChain();
const raw = result.Parameter?.Value;
if (raw === undefined || raw === null) {
throw blocksError(AppSettingErrors.ParameterNotFound, `Parameter "${getSdkIdentifiers(this).parameterName}" has no value`);
throw blocksError(AppSettingErrors.ParameterNotFound, `Parameter "${parameterName}" has no value`);
}

let value: T;
Expand Down
Loading