Skip to content

Commit 7f378b6

Browse files
feat(kinesis): throw ValidationErrors instead of untyped Errors (#34239)
### Issue # (if applicable) None ### Reason for this change Relates to #32569 ### Description of changes Untyped Errors are not recommended. ### 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 bc3e5aa commit 7f378b6

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const enableNoThrowDefaultErrorIn = [
6060
'aws-elasticloadbalancingv2-actions',
6161
'aws-elasticloadbalancingv2-targets',
6262
'aws-fsx',
63+
'aws-kinesis',
6364
'aws-kinesisfirehose',
6465
'aws-lambda',
6566
'aws-logs',

packages/aws-cdk-lib/aws-kinesis/lib/resource-policy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CfnResourcePolicy } from './kinesis.generated';
33
import { IStream } from './stream';
44
import { IStreamConsumer } from './stream-consumer';
55
import { PolicyDocument } from '../../aws-iam';
6-
import { Resource } from '../../core';
6+
import { Resource, ValidationError } from '../../core';
77
import { addConstructMetadata } from '../../core/lib/metadata-resource';
88

99
/**
@@ -62,10 +62,10 @@ export class ResourcePolicy extends Resource {
6262
addConstructMetadata(this, props);
6363

6464
if (props.stream && props.streamConsumer) {
65-
throw new Error('Only one of stream or streamConsumer can be set');
65+
throw new ValidationError('Only one of stream or streamConsumer can be set', this);
6666
}
6767
if (props.stream === undefined && props.streamConsumer === undefined) {
68-
throw new Error('One of stream or streamConsumer must be set');
68+
throw new ValidationError('One of stream or streamConsumer must be set', this);
6969
}
7070

7171
this.document = props.policyDocument ?? this.document;

packages/aws-cdk-lib/aws-kinesis/lib/stream.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ResourcePolicy } from './resource-policy';
55
import * as cloudwatch from '../../aws-cloudwatch';
66
import * as iam from '../../aws-iam';
77
import * as kms from '../../aws-kms';
8-
import { ArnFormat, Aws, CfnCondition, Duration, Fn, IResolvable, IResource, RemovalPolicy, Resource, ResourceProps, Stack, Token } from '../../core';
8+
import { ArnFormat, Aws, CfnCondition, Duration, Fn, IResolvable, IResource, RemovalPolicy, Resource, ResourceProps, Stack, Token, ValidationError } from '../../core';
99
import { addConstructMetadata } from '../../core/lib/metadata-resource';
1010

1111
const READ_OPERATIONS = [
@@ -841,7 +841,7 @@ export class Stream extends StreamBase {
841841
const streamMode = props.streamMode;
842842

843843
if (streamMode === StreamMode.ON_DEMAND && shardCount !== undefined) {
844-
throw new Error(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`);
844+
throw new ValidationError(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`, this);
845845
}
846846
if ((streamMode === StreamMode.PROVISIONED || streamMode === undefined) && shardCount === undefined) {
847847
shardCount = 1;
@@ -850,7 +850,7 @@ export class Stream extends StreamBase {
850850
const retentionPeriodHours = props.retentionPeriod?.toHours() ?? 24;
851851
if (!Token.isUnresolved(retentionPeriodHours)) {
852852
if (retentionPeriodHours < 24 || retentionPeriodHours > 8760) {
853-
throw new Error(`retentionPeriod must be between 24 and 8760 hours. Received ${retentionPeriodHours}`);
853+
throw new ValidationError(`retentionPeriod must be between 24 and 8760 hours. Received ${retentionPeriodHours}`, this);
854854
}
855855
}
856856

@@ -915,7 +915,7 @@ export class Stream extends StreamBase {
915915

916916
// if encryption key is set, encryption must be set to KMS.
917917
if (encryptionType !== StreamEncryption.KMS && props.encryptionKey) {
918-
throw new Error(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType})`);
918+
throw new ValidationError(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType})`, this);
919919
}
920920

921921
if (encryptionType === StreamEncryption.UNENCRYPTED) {
@@ -939,7 +939,7 @@ export class Stream extends StreamBase {
939939
return { encryptionKey, streamEncryption };
940940
}
941941

942-
throw new Error(`Unexpected 'encryptionType': ${encryptionType}`);
942+
throw new ValidationError(`Unexpected 'encryptionType': ${encryptionType}`, this);
943943
}
944944
}
945945

0 commit comments

Comments
 (0)