Skip to content

Commit b81df8d

Browse files
committed
fix(region-info): use local error class for standalone package usage
Introduce a local `RegionFactError` class that uses `CONSTRUCT_ERROR_SYMBOL` instead of importing `UnscopedValidationError` from an internal aws-cdk-lib path. This allows `@aws-cdk/region-info` to work standalone without requiring `aws-cdk-lib` as a runtime dependency while maintaining typed error support.
1 parent cf1d35e commit b81df8d

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

  • packages/aws-cdk-lib/region-info/lib

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { AWS_REGIONS } from './aws-entities';
2-
import { UnscopedValidationError } from '../../core/lib/errors';
2+
3+
const CONSTRUCT_ERROR_SYMBOL = Symbol.for('@aws-cdk/core.SynthesisError');
4+
5+
/**
6+
* An error thrown when a region fact operation fails.
7+
*/
8+
class RegionFactError extends Error {
9+
constructor(msg: string) {
10+
super(msg);
11+
Object.setPrototypeOf(this, RegionFactError.prototype);
12+
Object.defineProperty(this, CONSTRUCT_ERROR_SYMBOL, { value: true });
13+
this.name = 'RegionFactError';
14+
}
15+
}
316

417
/**
518
* A database of regional information.
@@ -57,7 +70,7 @@ export class Fact {
5770
const foundFact = this.find(region, name);
5871

5972
if (!foundFact) {
60-
throw new UnscopedValidationError(`No fact ${name} could be found for region: ${region} and name: ${name}.`);
73+
throw new RegionFactError(`No fact ${name} could be found for region: ${region} and name: ${name}.`);
6174
}
6275

6376
return foundFact;
@@ -72,7 +85,7 @@ export class Fact {
7285
public static register(fact: IFact, allowReplacing = false): void {
7386
const regionFacts = this.database[fact.region] || (this.database[fact.region] = {});
7487
if (fact.name in regionFacts && regionFacts[fact.name] !== fact.value && !allowReplacing) {
75-
throw new UnscopedValidationError(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
88+
throw new RegionFactError(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
7689
}
7790
if (fact.value !== undefined) {
7891
regionFacts[fact.name] = fact.value;
@@ -89,15 +102,17 @@ export class Fact {
89102
public static unregister(region: string, name: string, value?: string): void {
90103
const regionFacts = this.database[region] || {};
91104
if (name in regionFacts && value && regionFacts[name] !== value) {
92-
throw new UnscopedValidationError(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
105+
throw new RegionFactError(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
93106
}
94107
delete regionFacts[name];
95108
}
96109

97110
private static readonly database: { [region: string]: { [name: string]: string } } = {};
98111

99112
private constructor() {
100-
throw new UnscopedValidationError('Use the static methods of Fact instead!');
113+
// this should never happen, so throw a regular error here
114+
/* eslint-disable-next-line @cdklabs/no-throw-default-error */
115+
throw new Error('Use the static methods of Fact instead!');
101116
}
102117
}
103118

0 commit comments

Comments
 (0)