Skip to content

feat(region-info): throw ValidationError instead of untyped Errors #33384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const enableNoThrowDefaultErrorIn = [
'aws-s3objectlambda',
'aws-s3outposts',
'aws-s3tables',
'region-info',
'pipelines',
];
baseConfig.overrides.push({
Expand Down
6 changes: 4 additions & 2 deletions packages/aws-cdk-lib/region-info/lib/aws-entities.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnscopedValidationError because this module doesn't operate on scopes

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UnscopedValidationError } from '../../core';

/**
* After this point, S3 website domains look like `s3-website.REGION.s3.amazonaws.com`
*
Expand Down Expand Up @@ -86,7 +88,7 @@ export const AWS_REGIONS = AWS_REGIONS_AND_RULES
export function before(region: string, ruleOrRegion: string | symbol) {
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
if (ruleIx === -1) {
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
throw new UnscopedValidationError(`Unknown rule: ${String(ruleOrRegion)}`);
}
const regionIx = AWS_REGIONS_AND_RULES.indexOf(region);
return regionIx === -1 ? false : regionIx < ruleIx;
Expand All @@ -98,7 +100,7 @@ export function before(region: string, ruleOrRegion: string | symbol) {
export function regionsBefore(ruleOrRegion: string | symbol): string[] {
const ruleIx = AWS_REGIONS_AND_RULES.indexOf(ruleOrRegion);
if (ruleIx === -1) {
throw new Error(`Unknown rule: ${String(ruleOrRegion)}`);
throw new UnscopedValidationError(`Unknown rule: ${String(ruleOrRegion)}`);
}
return AWS_REGIONS_AND_RULES.slice(0, ruleIx)
.filter((entry) => typeof entry === 'string')
Expand Down
9 changes: 5 additions & 4 deletions packages/aws-cdk-lib/region-info/lib/fact.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnscopedValidationError because this module doesn't operate on scopes

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AWS_REGIONS } from './aws-entities';
import { UnscopedValidationError } from '../../core';

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

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

return foundFact;
Expand All @@ -71,7 +72,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 Error(`Region ${fact.region} already has a fact ${fact.name}, with value ${regionFacts[fact.name]}`);
throw new UnscopedValidationError(`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 @@ -88,15 +89,15 @@ 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 Error(`Attempted to remove ${name} from ${region} with value ${value}, but the fact's value is ${regionFacts[name]}`);
throw new UnscopedValidationError(`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 Error('Use the static methods of Fact instead!');
throw new UnscopedValidationError('Use the static methods of Fact instead!');
}
}

Expand Down
Loading