Skip to content
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

feat(region-info): throw exception when no fact found #5166

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions packages/@aws-cdk/region-info/lib/fact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ export class Fact {
return regionFacts && regionFacts[name];
}

/**
* Retrieve a fact from the Fact database. (retrieval will fail if the specified region or
* fact name does not exist.)
*
* @param region the name of the region (e.g: `us-east-1`)
* @param name the name of the fact being looked up (see the `FactName` class for details)
*/
public static requireFact(region: string, name: string): string {
const foundFact = this.find(region, name);

if (!foundFact) {
throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}`);
}

return foundFact;
}

/**
* Registers a new fact in this Fact database.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/region-info/test/fact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ describe('find', () => {
});
});

describe('requireFact', () => {
test('throws error for an unknown fact', () => {
expect(() => Fact.requireFact(AWS_REGIONS[0], 'not:a:known:fact')).toThrowError();
});

test('throws error for an unknown region', () => {
expect(() => Fact.requireFact('bermuda-triangle-42', FactName.PARTITION)).toThrowError();
});
});

describe('register', () => {
test('allows registering an arbitrary fact', () => {
// GIVEN
Expand Down