Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/aws-cdk-lib/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import { makeConfig } from '@aws-cdk/eslint-config';
import { defineConfig } from 'eslint/config';

export default makeConfig('tsconfig.json');
export default defineConfig(
makeConfig('tsconfig.json'),
{
files: ['region-info/lib/**'],
rules: {
'no-restricted-imports': ['error', {
patterns: [{
group: ['../../*'],
message: '@aws-cdk/region-info must work standalone without aws-cdk-lib imports',
}],
}],
},
},
);
25 changes: 20 additions & 5 deletions packages/aws-cdk-lib/region-info/lib/fact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { AWS_REGIONS } from './aws-entities';
import { UnscopedValidationError } from '../../core/lib/errors';

const CONSTRUCT_ERROR_SYMBOL = Symbol.for('@aws-cdk/core.SynthesisError');

/**
* An error thrown when a region fact operation fails.
*/
class RegionFactError extends Error {
constructor(msg: string) {
super(msg);
Object.setPrototypeOf(this, RegionFactError.prototype);
Object.defineProperty(this, CONSTRUCT_ERROR_SYMBOL, { value: true });
this.name = 'RegionFactError';
}
}

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

if (!foundFact) {
throw new UnscopedValidationError(`No fact ${name} could be found for region: ${region} and name: ${name}.`);
throw new RegionFactError(`No fact ${name} could be found for region: ${region} and name: ${name}.`);
}

return foundFact;
Expand All @@ -72,7 +85,7 @@ export class Fact {
public static register(fact: IFact, allowReplacing = false): void {
const regionFacts = this.database[fact.region] || (this.database[fact.region] = {});
if (fact.name in regionFacts && regionFacts[fact.name] !== fact.value && !allowReplacing) {
throw new UnscopedValidationError(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
throw new RegionFactError(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
}
if (fact.value !== undefined) {
regionFacts[fact.name] = fact.value;
Expand All @@ -89,15 +102,17 @@ export class Fact {
public static unregister(region: string, name: string, value?: string): void {
const regionFacts = this.database[region] || {};
if (name in regionFacts && value && regionFacts[name] !== value) {
throw new UnscopedValidationError(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
throw new RegionFactError(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
}
delete regionFacts[name];
}

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

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

Expand Down
Loading