Skip to content

Commit

Permalink
refactor: undefined instead of throw when export not found
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwwright committed Sep 26, 2023
1 parent 8cb7e2b commit 0a78d8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
9 changes: 6 additions & 3 deletions packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class LazyListStackResources implements ListStackResources {
}

export interface LookupExport {
lookupExport(name: string): Promise<AWS.CloudFormation.Export>;
lookupExport(name: string): Promise<AWS.CloudFormation.Export | undefined>;
}

export class LookupExportError extends Error { }
Expand All @@ -45,7 +45,7 @@ export class LazyLookupExport implements LookupExport {

constructor(private readonly sdk: ISDK) { }

async lookupExport(name: string): Promise<AWS.CloudFormation.Export> {
async lookupExport(name: string): Promise<AWS.CloudFormation.Export | undefined> {
if (this.exports[name]) {
return this.exports[name];
}
Expand All @@ -63,7 +63,7 @@ export class LazyLookupExport implements LookupExport {
}
}

throw new LookupExportError(`CloudFormation Export cannot be resolved as it does not exist: ${name}`);
return undefined; // export not found
}

private async * listExports() {
Expand Down Expand Up @@ -253,6 +253,9 @@ export class EvaluateCloudFormationTemplate {

async 'Fn::ImportValue'(name: string): Promise<string> {
const exported = await self.lookupExport.lookupExport(name);
if (!exported) {
throw new CfnEvaluationException(`Export '${name}' could not be found for evaluation`);
}
if (!exported.Value) {
throw new CfnEvaluationException(`Export '${name}' exists without a value`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CfnEvaluationException,
EvaluateCloudFormationTemplate,
LazyListStackResources,
LazyLookupExport,
Expand Down Expand Up @@ -106,8 +107,7 @@ describe('evaluateCfnExpression', () => {
const evaluate = () => evaluateCfnTemplate.evaluateCfnExpression({
'Fn::ImportValue': 'blah',
});

await expect(evaluate).rejects.toBeInstanceOf(LookupExportError);
await expect(evaluate).rejects.toBeInstanceOf(CfnEvaluationException);
});
});
});
9 changes: 5 additions & 4 deletions packages/aws-cdk/test/api/lazy-lookup-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe('LazyLookupExport', () => {
it('returns the matching export', async () => {
const name = 'test-name-3';
const result = await lookup.lookupExport(name);
expect(result.Name).toEqual(name);
expect(result.Value).toEqual('test-value-3');
expect(result?.Name).toEqual(name);
expect(result?.Value).toEqual('test-value-3');
});

it('stops fetching once export is found', async () => {
Expand All @@ -76,8 +76,9 @@ describe('LazyLookupExport', () => {
expect(listExports).toHaveBeenCalledTimes(1);
});

it('throws if the export does not exist', async () => {
await expect(() => lookup.lookupExport('test-name-unknown')).rejects.toBeInstanceOf(LookupExportError);
it('returns undefined if the export does not exist', async () => {
const result = await lookup.lookupExport('test-name-unknown');
expect(result).toBeUndefined();
});
});
});

0 comments on commit 0a78d8a

Please sign in to comment.