From 496d2e153666be7d49e23cb42c2488e8929bf84b Mon Sep 17 00:00:00 2001 From: Kazuho Cryer-Shinozuka Date: Thu, 24 Apr 2025 21:44:28 +0900 Subject: [PATCH] typed error --- packages/aws-cdk-lib/.eslintrc.js | 1 + .../aws-cdk-lib/aws-kinesis/lib/resource-policy.ts | 6 +++--- packages/aws-cdk-lib/aws-kinesis/lib/stream.ts | 10 +++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/aws-cdk-lib/.eslintrc.js b/packages/aws-cdk-lib/.eslintrc.js index 524615424a287..a1cd96ad9d61a 100644 --- a/packages/aws-cdk-lib/.eslintrc.js +++ b/packages/aws-cdk-lib/.eslintrc.js @@ -60,6 +60,7 @@ const enableNoThrowDefaultErrorIn = [ 'aws-elasticloadbalancingv2-actions', 'aws-elasticloadbalancingv2-targets', 'aws-fsx', + 'aws-kinesis', 'aws-kinesisfirehose', 'aws-lambda', 'aws-logs', diff --git a/packages/aws-cdk-lib/aws-kinesis/lib/resource-policy.ts b/packages/aws-cdk-lib/aws-kinesis/lib/resource-policy.ts index 5de838ac3fb36..e905112066f69 100644 --- a/packages/aws-cdk-lib/aws-kinesis/lib/resource-policy.ts +++ b/packages/aws-cdk-lib/aws-kinesis/lib/resource-policy.ts @@ -3,7 +3,7 @@ import { CfnResourcePolicy } from './kinesis.generated'; import { IStream } from './stream'; import { IStreamConsumer } from './stream-consumer'; import { PolicyDocument } from '../../aws-iam'; -import { Resource } from '../../core'; +import { Resource, ValidationError } from '../../core'; import { addConstructMetadata } from '../../core/lib/metadata-resource'; /** @@ -62,10 +62,10 @@ export class ResourcePolicy extends Resource { addConstructMetadata(this, props); if (props.stream && props.streamConsumer) { - throw new Error('Only one of stream or streamConsumer can be set'); + throw new ValidationError('Only one of stream or streamConsumer can be set', this); } if (props.stream === undefined && props.streamConsumer === undefined) { - throw new Error('One of stream or streamConsumer must be set'); + throw new ValidationError('One of stream or streamConsumer must be set', this); } this.document = props.policyDocument ?? this.document; diff --git a/packages/aws-cdk-lib/aws-kinesis/lib/stream.ts b/packages/aws-cdk-lib/aws-kinesis/lib/stream.ts index b17638f9dbc94..c05e6d5c04c62 100644 --- a/packages/aws-cdk-lib/aws-kinesis/lib/stream.ts +++ b/packages/aws-cdk-lib/aws-kinesis/lib/stream.ts @@ -5,7 +5,7 @@ import { ResourcePolicy } from './resource-policy'; import * as cloudwatch from '../../aws-cloudwatch'; import * as iam from '../../aws-iam'; import * as kms from '../../aws-kms'; -import { ArnFormat, Aws, CfnCondition, Duration, Fn, IResolvable, IResource, RemovalPolicy, Resource, ResourceProps, Stack, Token } from '../../core'; +import { ArnFormat, Aws, CfnCondition, Duration, Fn, IResolvable, IResource, RemovalPolicy, Resource, ResourceProps, Stack, Token, ValidationError } from '../../core'; import { addConstructMetadata } from '../../core/lib/metadata-resource'; const READ_OPERATIONS = [ @@ -841,7 +841,7 @@ export class Stream extends StreamBase { const streamMode = props.streamMode; if (streamMode === StreamMode.ON_DEMAND && shardCount !== undefined) { - throw new Error(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`); + throw new ValidationError(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`, this); } if ((streamMode === StreamMode.PROVISIONED || streamMode === undefined) && shardCount === undefined) { shardCount = 1; @@ -850,7 +850,7 @@ export class Stream extends StreamBase { const retentionPeriodHours = props.retentionPeriod?.toHours() ?? 24; if (!Token.isUnresolved(retentionPeriodHours)) { if (retentionPeriodHours < 24 || retentionPeriodHours > 8760) { - throw new Error(`retentionPeriod must be between 24 and 8760 hours. Received ${retentionPeriodHours}`); + throw new ValidationError(`retentionPeriod must be between 24 and 8760 hours. Received ${retentionPeriodHours}`, this); } } @@ -915,7 +915,7 @@ export class Stream extends StreamBase { // if encryption key is set, encryption must be set to KMS. if (encryptionType !== StreamEncryption.KMS && props.encryptionKey) { - throw new Error(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType})`); + throw new ValidationError(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType})`, this); } if (encryptionType === StreamEncryption.UNENCRYPTED) { @@ -939,7 +939,7 @@ export class Stream extends StreamBase { return { encryptionKey, streamEncryption }; } - throw new Error(`Unexpected 'encryptionType': ${encryptionType}`); + throw new ValidationError(`Unexpected 'encryptionType': ${encryptionType}`, this); } }