diff --git a/packages/@aws-cdk/region-info/lib/fact.ts b/packages/@aws-cdk/region-info/lib/fact.ts index 90cae4c079926..355bd9f855b1f 100644 --- a/packages/@aws-cdk/region-info/lib/fact.ts +++ b/packages/@aws-cdk/region-info/lib/fact.ts @@ -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. * diff --git a/packages/@aws-cdk/region-info/test/fact.test.ts b/packages/@aws-cdk/region-info/test/fact.test.ts index 382502f8a3bcf..5522d4bd16d1d 100644 --- a/packages/@aws-cdk/region-info/test/fact.test.ts +++ b/packages/@aws-cdk/region-info/test/fact.test.ts @@ -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