Skip to content

Commit 303daf2

Browse files
authored
feat(region-info): throw ValidationError instead of untyped Errors (#33384)
### Issue Relates to #32569 ### Description of changes `ValidationErrors` where apprporiate Note that files in the `build-tools` directory are excluded from the linter rules, since they are only used at build time. I moved the `before` function to `build-tools` because it's not exported and only used at build time. Importing the `core` module to a build-time script doesn't work as it would add A LOT of additional files to the build-time hot path and that just completely blows the build script up 💥 (aside from it being totally unnecessary anyway). ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Existing tests. Exemptions granted as this is a refactor of existing code. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9c68ec2 commit 303daf2

File tree

4 files changed

+21
-34
lines changed

4 files changed

+21
-34
lines changed

packages/aws-cdk-lib/.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error'];
1818
// not yet supported
1919
const noThrowDefaultErrorNotYetSupported = [
2020
'core',
21-
'region-info',
2221
];
2322
baseConfig.overrides.push({
2423
files: [

packages/aws-cdk-lib/region-info/build-tools/generate-static-data.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import {
1818
import { AWS_CDK_METADATA } from './metadata';
1919
import {
2020
AWS_REGIONS,
21-
before,
2221
RULE_S3_WEBSITE_REGIONAL_SUBDOMAIN,
2322
RULE_CLASSIC_PARTITION_BECOMES_OPT_IN,
23+
AWS_REGIONS_AND_RULES,
2424
} from '../lib/aws-entities';
2525

2626
export async function main(): Promise<void> {
@@ -169,10 +169,24 @@ function checkRegionsSubMap(map: Record<string, Record<string, Record<string, un
169169
}
170170
}
171171

172-
export function after(region: string, ruleOrRegion: string | symbol) {
172+
function after(region: string, ruleOrRegion: string | symbol) {
173173
return region !== ruleOrRegion && !before(region, ruleOrRegion);
174174
}
175175

176+
/**
177+
* Whether or not a region predates a given rule (or region).
178+
*
179+
* Unknown region => we have to assume no.
180+
*/
181+
function before(region: string, ruleOrRegion: string | symbol) {
182+
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
183+
if (ruleIx === -1) {
184+
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
185+
}
186+
const regionIx = AWS_REGIONS_AND_RULES.indexOf(region);
187+
return regionIx === -1 ? false : regionIx < ruleIx;
188+
}
189+
176190
main().catch(e => {
177191
// eslint-disable-next-line no-console
178192
console.error(e);

packages/aws-cdk-lib/region-info/lib/aws-entities.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,6 @@ export const AWS_REGIONS = AWS_REGIONS_AND_RULES
8080
.filter((x) => typeof x === 'string')
8181
.sort() as readonly string[];
8282

83-
/**
84-
* Whether or not a region predates a given rule (or region).
85-
*
86-
* Unknown region => we have to assume no.
87-
*/
88-
export function before(region: string, ruleOrRegion: string | symbol) {
89-
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
90-
if (ruleIx === -1) {
91-
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
92-
}
93-
const regionIx = AWS_REGIONS_AND_RULES.indexOf(region);
94-
return regionIx === -1 ? false : regionIx < ruleIx;
95-
}
96-
97-
/**
98-
* Return all regions before a given rule was introduced (or region)
99-
*/
100-
export function regionsBefore(ruleOrRegion: string | symbol): string[] {
101-
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
102-
if (ruleIx === -1) {
103-
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
104-
}
105-
return AWS_REGIONS_AND_RULES.slice(0, ruleIx)
106-
.filter((entry) => typeof entry === 'string')
107-
.sort() as string[];
108-
}
109-
11083
export interface Region { readonly partition: string; readonly domainSuffix: string }
11184

11285
const PARTITION_MAP: {readonly [region: string]: Region } = {

packages/aws-cdk-lib/region-info/lib/fact.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AWS_REGIONS } from './aws-entities';
2+
import { UnscopedValidationError } from '../../core/lib/errors';
23

34
/**
45
* A database of regional information.
@@ -56,7 +57,7 @@ export class Fact {
5657
const foundFact = this.find(region, name);
5758

5859
if (!foundFact) {
59-
throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}.`);
60+
throw new UnscopedValidationError(`No fact ${name} could be found for region: ${region} and name: ${name}.`);
6061
}
6162

6263
return foundFact;
@@ -71,7 +72,7 @@ export class Fact {
7172
public static register(fact: IFact, allowReplacing = false): void {
7273
const regionFacts = this.database[fact.region] || (this.database[fact.region] = {});
7374
if (fact.name in regionFacts && regionFacts[fact.name] !== fact.value && !allowReplacing) {
74-
throw new Error(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
75+
throw new UnscopedValidationError(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
7576
}
7677
if (fact.value !== undefined) {
7778
regionFacts[fact.name] = fact.value;
@@ -88,15 +89,15 @@ export class Fact {
8889
public static unregister(region: string, name: string, value?: string): void {
8990
const regionFacts = this.database[region] || {};
9091
if (name in regionFacts && value && regionFacts[name] !== value) {
91-
throw new Error(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
92+
throw new UnscopedValidationError(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
9293
}
9394
delete regionFacts[name];
9495
}
9596

9697
private static readonly database: { [region: string]: { [name: string]: string } } = {};
9798

9899
private constructor() {
99-
throw new Error('Use the static methods of Fact instead!');
100+
throw new UnscopedValidationError('Use the static methods of Fact instead!');
100101
}
101102
}
102103

0 commit comments

Comments
 (0)