Skip to content

Commit 5bc9292

Browse files
authored
feat(config): throw ValidationErrors instead of untyped Errors (#33869)
### Issue # (if applicable) Relates to #32569 ### Reason for this change untyped Errors are not recommended ### Description of changes ValidationErrors everywhere ### Describe any new or updated permissions being added None ### 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 731137a commit 5bc9292

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const enableNoThrowDefaultErrorIn = [
4646
'aws-codepipeline',
4747
'aws-codepipeline-actions',
4848
'aws-cognito',
49+
'aws-config',
4950
'aws-docdb',
5051
'aws-ecr',
5152
'aws-efs',

packages/aws-cdk-lib/aws-config/lib/managed-rules.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Construct } from 'constructs';
22
import { ManagedRule, ManagedRuleIdentifiers, ResourceType, RuleProps, RuleScope } from './rule';
33
import * as iam from '../../aws-iam';
44
import * as sns from '../../aws-sns';
5-
import { Duration, Lazy, Stack } from '../../core';
5+
import { Duration, Lazy, Stack, ValidationError } from '../../core';
66
import { addConstructMetadata } from '../../core/lib/metadata-resource';
77

88
/**
@@ -121,7 +121,7 @@ export interface CloudFormationStackNotificationCheckProps extends RuleProps {
121121
export class CloudFormationStackNotificationCheck extends ManagedRule {
122122
constructor(scope: Construct, id: string, props: CloudFormationStackNotificationCheckProps = {}) {
123123
if (props.topics && props.topics.length > 5) {
124-
throw new Error('At most 5 topics can be specified.');
124+
throw new ValidationError('At most 5 topics can be specified.', scope);
125125
}
126126

127127
super(scope, id, {

packages/aws-cdk-lib/aws-config/lib/rule.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CfnConfigRule } from './config.generated';
44
import * as events from '../../aws-events';
55
import * as iam from '../../aws-iam';
66
import * as lambda from '../../aws-lambda';
7-
import { IResource, Lazy, Resource, Stack } from '../../core';
7+
import { IResource, Lazy, Resource, Stack, ValidationError } from '../../core';
88
import { addConstructMetadata } from '../../core/lib/metadata-resource';
99

1010
/**
@@ -424,7 +424,7 @@ export class CustomRule extends RuleNew {
424424
addConstructMetadata(this, props);
425425

426426
if (!props.configurationChanges && !props.periodic) {
427-
throw new Error('At least one of `configurationChanges` or `periodic` must be set to true.');
427+
throw new ValidationError('At least one of `configurationChanges` or `periodic` must be set to true.', this);
428428
}
429429

430430
const sourceDetails: SourceDetail[] = [];
@@ -542,10 +542,10 @@ export class CustomPolicy extends RuleNew {
542542
addConstructMetadata(this, props);
543543

544544
if (!props.policyText || [...props.policyText].length === 0) {
545-
throw new Error('Policy Text cannot be empty.');
545+
throw new ValidationError('Policy Text cannot be empty.', this);
546546
}
547547
if ([...props.policyText].length > 10000) {
548-
throw new Error('Policy Text is limited to 10,000 characters or less.');
548+
throw new ValidationError('Policy Text is limited to 10,000 characters or less.', this);
549549
}
550550

551551
const sourceDetails: SourceDetail[] = [];

0 commit comments

Comments
 (0)