Skip to content

Commit 7147e75

Browse files
authored
feat(ses): throw ValidationErrors instead of untyped Errors (#34098)
### Issue # (if applicable) Relates to #32569 ### Reason for this change Untyped Errors are not recommended. ### Description of changes Change Error to ValidationError / UnscopedValidationError ### 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 ff10172 commit 7147e75

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const enableNoThrowDefaultErrorIn = [
6262
'aws-logs',
6363
'aws-rds',
6464
'aws-s3',
65+
'aws-ses',
6566
'aws-sns',
6667
'aws-sqs',
6768
'aws-ssm',

packages/aws-cdk-lib/aws-ses-actions/lib/add-header.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ses from '../../aws-ses';
2+
import { UnscopedValidationError } from '../../core';
23

34
/**
45
* Construction properties for a add header action.
@@ -28,11 +29,11 @@ export class AddHeader implements ses.IReceiptRuleAction {
2829
constructor(props: AddHeaderProps) {
2930
if (!/^[a-zA-Z0-9-]{1,50}$/.test(props.name)) {
3031
// eslint-disable-next-line max-len
31-
throw new Error('Header `name` must be between 1 and 50 characters, inclusive, and consist of alphanumeric (a-z, A-Z, 0-9) characters and dashes only.');
32+
throw new UnscopedValidationError('Header `name` must be between 1 and 50 characters, inclusive, and consist of alphanumeric (a-z, A-Z, 0-9) characters and dashes only.');
3233
}
3334

3435
if (!/^[^\n\r]{0,2047}$/.test(props.value)) {
35-
throw new Error('Header `value` must be less than 2048 characters, and must not contain newline characters ("\r" or "\n").');
36+
throw new UnscopedValidationError('Header `value` must be less than 2048 characters, and must not contain newline characters ("\r" or "\n").');
3637
}
3738

3839
this.name = props.name;

packages/aws-cdk-lib/aws-ses/lib/configuration-set-event-destination.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as events from '../../aws-events';
55
import * as iam from '../../aws-iam';
66
import * as firehose from '../../aws-kinesisfirehose';
77
import * as sns from '../../aws-sns';
8-
import { Aws, IResource, Resource, Stack } from '../../core';
8+
import { Aws, IResource, Resource, Stack, ValidationError } from '../../core';
99
import { addConstructMetadata } from '../../core/lib/metadata-resource';
1010

1111
/**
@@ -296,7 +296,7 @@ export class ConfigurationSetEventDestination extends Resource implements IConfi
296296
resourceName: 'default',
297297
})
298298
) {
299-
throw new Error(`Only the default bus can be used as an event destination. Got ${props.destination.bus.eventBusArn}`);
299+
throw new ValidationError(`Only the default bus can be used as an event destination. Got ${props.destination.bus.eventBusArn}`, this);
300300
}
301301

302302
let firehoseDeliveryStreamIamRoleArn = '';

packages/aws-cdk-lib/aws-ses/lib/configuration-set.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigurationSetEventDestination, ConfigurationSetEventDestinationOptio
33
import { IDedicatedIpPool } from './dedicated-ip-pool';
44
import { undefinedIfNoKeys } from './private/utils';
55
import { CfnConfigurationSet } from './ses.generated';
6-
import { Duration, IResource, Resource, Token } from '../../core';
6+
import { Duration, IResource, Resource, Token, ValidationError } from '../../core';
77
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
88

99
/**
@@ -179,14 +179,14 @@ export class ConfigurationSet extends Resource implements IConfigurationSet {
179179
addConstructMetadata(this, props);
180180

181181
if (props.disableSuppressionList && props.suppressionReasons) {
182-
throw new Error('When disableSuppressionList is true, suppressionReasons must not be specified.');
182+
throw new ValidationError('When disableSuppressionList is true, suppressionReasons must not be specified.', this);
183183
}
184184
if (props.maxDeliveryDuration && !Token.isUnresolved(props.maxDeliveryDuration)) {
185185
if (props.maxDeliveryDuration.toMilliseconds() < Duration.minutes(5).toMilliseconds()) {
186-
throw new Error(`The maximum delivery duration must be greater than or equal to 5 minutes (300_000 milliseconds), got: ${props.maxDeliveryDuration.toMilliseconds()} milliseconds.`);
186+
throw new ValidationError(`The maximum delivery duration must be greater than or equal to 5 minutes (300_000 milliseconds), got: ${props.maxDeliveryDuration.toMilliseconds()} milliseconds.`, this);
187187
}
188188
if (props.maxDeliveryDuration.toSeconds() > Duration.hours(14).toSeconds()) {
189-
throw new Error(`The maximum delivery duration must be less than or equal to 14 hours (50400 seconds), got: ${props.maxDeliveryDuration.toSeconds()} seconds.`);
189+
throw new ValidationError(`The maximum delivery duration must be less than or equal to 14 hours (50400 seconds), got: ${props.maxDeliveryDuration.toSeconds()} seconds.`, this);
190190
}
191191
}
192192

packages/aws-cdk-lib/aws-ses/lib/dedicated-ip-pool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Construct } from 'constructs';
22
import { CfnDedicatedIpPool } from './ses.generated';
3-
import { IResource, Resource } from '../../core';
3+
import { IResource, Resource, ValidationError } from '../../core';
44
import { addConstructMetadata } from '../../core/lib/metadata-resource';
55

66
/**
@@ -82,7 +82,7 @@ export class DedicatedIpPool extends Resource implements IDedicatedIpPool {
8282
addConstructMetadata(this, props);
8383

8484
if (props.dedicatedIpPoolName && !/^[a-z0-9_-]{0,64}$/.test(props.dedicatedIpPoolName)) {
85-
throw new Error(`Invalid dedicatedIpPoolName "${props.dedicatedIpPoolName}". The name must only include lowercase letters, numbers, underscores, hyphens, and must not exceed 64 characters.`);
85+
throw new ValidationError(`Invalid dedicatedIpPoolName "${props.dedicatedIpPoolName}". The name must only include lowercase letters, numbers, underscores, hyphens, and must not exceed 64 characters.`, this);
8686
}
8787

8888
const pool = new CfnDedicatedIpPool(this, 'Resource', {

0 commit comments

Comments
 (0)