Skip to content

Commit 12b1a74

Browse files
committed
feat(region-info): throw ValidationError instead of untyped Errors
1 parent d8e6c09 commit 12b1a74

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const noThrowDefaultErrorNotYetSupported = [
2525
'aws-servicecatalog',
2626
'core',
2727
'custom-resources',
28-
'region-info',
2928
];
3029
baseConfig.overrides.push({
3130
files: [

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UnscopedValidationError } from '../../core';
2+
13
/**
24
* After this point, S3 website domains look like `s3-website.REGION.s3.amazonaws.com`
35
*
@@ -88,7 +90,7 @@ export const AWS_REGIONS = AWS_REGIONS_AND_RULES
8890
export function before(region: string, ruleOrRegion: string | symbol) {
8991
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
9092
if (ruleIx === -1) {
91-
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
93+
throw new UnscopedValidationError(`Unknown rule: ${String(ruleOrRegion)}`);
9294
}
9395
const regionIx = AWS_REGIONS_AND_RULES.indexOf(region);
9496
return regionIx === -1 ? false : regionIx < ruleIx;
@@ -100,7 +102,7 @@ export function before(region: string, ruleOrRegion: string | symbol) {
100102
export function regionsBefore(ruleOrRegion: string | symbol): string[] {
101103
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
102104
if (ruleIx === -1) {
103-
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
105+
throw new UnscopedValidationError(`Unknown rule: ${String(ruleOrRegion)}`);
104106
}
105107
return AWS_REGIONS_AND_RULES.slice(0, ruleIx)
106108
.filter((entry) => typeof entry === 'string')

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';
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)