Skip to content

Commit

Permalink
feat(region-info): throw exception when no fact found (#5166)
Browse files Browse the repository at this point in the history
* New method that throws an exception when the Fact value, given a name and region, are not found.

closes #3194
  • Loading branch information
stephendwu authored and mergify[bot] committed Dec 10, 2019
1 parent f445147 commit 88df1eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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

0 comments on commit 88df1eb

Please sign in to comment.