Skip to content

Commit 38f89af

Browse files
authored
feat(apprunner): throw ValidationError instead of untyped errors (#33914)
### Issue # (if applicable) `aws-apprunner-alpha` for #32569 ### Description of changes ValidationErrors everywhere ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Existing tests. Exemptions granted as this is basically a refactor of existing code. ### Checklist - [ ] 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 b9cb47c commit 38f89af

File tree

7 files changed

+46
-38
lines changed

7 files changed

+46
-38
lines changed

Diff for: packages/@aws-cdk/aws-apprunner-alpha/.eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@ baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
44
baseConfig.rules['import/no-extraneous-dependencies'] = ['error', { devDependencies: true, peerDependencies: true } ];
55
baseConfig.rules['import/order'] = 'off';
66
baseConfig.rules['@aws-cdk/invalid-cfn-imports'] = 'off';
7+
baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error'];
8+
baseConfig.overrides.push({
9+
files: ["./test/**"],
10+
rules: {
11+
"@cdklabs/no-throw-default-error": "off",
12+
},
13+
});
714

815
module.exports = baseConfig;

Diff for: packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali
118118
const resourceParts = cdk.Fn.split('/', autoScalingConfigurationArn);
119119

120120
if (!resourceParts || resourceParts.length < 3) {
121-
throw new Error(`Unexpected ARN format: ${autoScalingConfigurationArn}.`);
121+
throw new cdk.UnscopedValidationError(`Unexpected ARN format: ${autoScalingConfigurationArn}.`);
122122
}
123123

124124
const autoScalingConfigurationName = cdk.Fn.select(0, resourceParts);
@@ -175,14 +175,14 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali
175175
private validateAutoScalingConfiguration(props: AutoScalingConfigurationProps) {
176176
if (props.autoScalingConfigurationName !== undefined && !cdk.Token.isUnresolved(props.autoScalingConfigurationName)) {
177177
if (props.autoScalingConfigurationName.length < 4 || props.autoScalingConfigurationName.length > 32) {
178-
throw new Error(
179-
`\`autoScalingConfigurationName\` must be between 4 and 32 characters, got: ${props.autoScalingConfigurationName.length} characters.`,
178+
throw new cdk.ValidationError(
179+
`\`autoScalingConfigurationName\` must be between 4 and 32 characters, got: ${props.autoScalingConfigurationName.length} characters.`, this,
180180
);
181181
}
182182

183183
if (!/^[A-Za-z0-9][A-Za-z0-9\-_]*$/.test(props.autoScalingConfigurationName)) {
184-
throw new Error(
185-
`\`autoScalingConfigurationName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.autoScalingConfigurationName}.`,
184+
throw new cdk.ValidationError(
185+
`\`autoScalingConfigurationName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.autoScalingConfigurationName}.`, this,
186186
);
187187
}
188188
}
@@ -192,19 +192,19 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali
192192
const isMaxConcurrencyDefined = typeof props.maxConcurrency === 'number';
193193

194194
if (isMinSizeDefined && (props.minSize < 1 || props.minSize > 25)) {
195-
throw new Error(`minSize must be between 1 and 25, got ${props.minSize}.`);
195+
throw new cdk.ValidationError(`minSize must be between 1 and 25, got ${props.minSize}.`, this);
196196
}
197197

198198
if (isMaxSizeDefined && (props.maxSize < 1 || props.maxSize > 25)) {
199-
throw new Error(`maxSize must be between 1 and 25, got ${props.maxSize}.`);
199+
throw new cdk.ValidationError(`maxSize must be between 1 and 25, got ${props.maxSize}.`, this);
200200
}
201201

202202
if (isMinSizeDefined && isMaxSizeDefined && !(props.minSize < props.maxSize)) {
203-
throw new Error('maxSize must be greater than minSize.');
203+
throw new cdk.ValidationError('maxSize must be greater than minSize.', this);
204204
}
205205

206206
if (isMaxConcurrencyDefined && (props.maxConcurrency < 1 || props.maxConcurrency > 200)) {
207-
throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}.`);
207+
throw new cdk.ValidationError(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}.`, this);
208208
}
209209
}
210210
}

Diff for: packages/@aws-cdk/aws-apprunner-alpha/lib/observability-configuration.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class ObservabilityConfiguration extends cdk.Resource implements IObserva
104104
const resourceParts = cdk.Fn.split('/', observabilityConfigurationArn);
105105

106106
if (!resourceParts || resourceParts.length < 3) {
107-
throw new Error(`Unexpected ARN format: ${observabilityConfigurationArn}.`);
107+
throw new cdk.UnscopedValidationError(`Unexpected ARN format: ${observabilityConfigurationArn}.`);
108108
}
109109

110110
const observabilityConfigurationName = cdk.Fn.select(0, resourceParts);
@@ -146,14 +146,14 @@ export class ObservabilityConfiguration extends cdk.Resource implements IObserva
146146

147147
if (props.observabilityConfigurationName !== undefined && !cdk.Token.isUnresolved(props.observabilityConfigurationName)) {
148148
if (props.observabilityConfigurationName.length < 4 || props.observabilityConfigurationName.length > 32) {
149-
throw new Error(
150-
`\`observabilityConfigurationName\` must be between 4 and 32 characters, got: ${props.observabilityConfigurationName.length} characters.`,
149+
throw new cdk.ValidationError(
150+
`\`observabilityConfigurationName\` must be between 4 and 32 characters, got: ${props.observabilityConfigurationName.length} characters.`, this,
151151
);
152152
}
153153

154154
if (!/^[A-Za-z0-9][A-Za-z0-9\-_]*$/.test(props.observabilityConfigurationName)) {
155-
throw new Error(
156-
`\`observabilityConfigurationName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.observabilityConfigurationName}.`,
155+
throw new cdk.ValidationError(
156+
`\`observabilityConfigurationName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.observabilityConfigurationName}.`, this,
157157
);
158158
}
159159
}

Diff for: packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Cpu {
7373
(pattern) => pattern === unit,
7474
);
7575
if (!isValidValue) {
76-
throw new Error('CPU value is invalid');
76+
throw new cdk.UnscopedValidationError('CPU value is invalid');
7777
}
7878

7979
return new Cpu(unit);
@@ -150,7 +150,7 @@ export class Memory {
150150
(pattern) => pattern === unit,
151151
);
152152
if (!isValidValue) {
153-
throw new Error('Memory value is invalid');
153+
throw new cdk.UnscopedValidationError('Memory value is invalid');
154154
}
155155

156156
return new Memory(unit);
@@ -1019,24 +1019,24 @@ export class HealthCheck {
10191019
) {
10201020
if (this.healthCheckProtocolType === HealthCheckProtocolType.HTTP) {
10211021
if (this.path !== undefined && this.path.length === 0) {
1022-
throw new Error('path length must be greater than 0');
1022+
throw new cdk.UnscopedValidationError('path length must be greater than 0');
10231023
}
10241024
if (this.path === undefined) {
10251025
this.path = '/';
10261026
}
10271027
}
10281028

10291029
if (this.healthyThreshold < 1 || this.healthyThreshold > 20) {
1030-
throw new Error(`healthyThreshold must be between 1 and 20, got ${this.healthyThreshold}`);
1030+
throw new cdk.UnscopedValidationError(`healthyThreshold must be between 1 and 20, got ${this.healthyThreshold}`);
10311031
}
10321032
if (this.unhealthyThreshold < 1 || this.unhealthyThreshold > 20) {
1033-
throw new Error(`unhealthyThreshold must be between 1 and 20, got ${this.unhealthyThreshold}`);
1033+
throw new cdk.UnscopedValidationError(`unhealthyThreshold must be between 1 and 20, got ${this.unhealthyThreshold}`);
10341034
}
10351035
if (this.interval.toSeconds() < 1 || this.interval.toSeconds() > 20) {
1036-
throw new Error(`interval must be between 1 and 20 seconds, got ${this.interval.toSeconds()}`);
1036+
throw new cdk.UnscopedValidationError(`interval must be between 1 and 20 seconds, got ${this.interval.toSeconds()}`);
10371037
}
10381038
if (this.timeout.toSeconds() < 1 || this.timeout.toSeconds() > 20) {
1039-
throw new Error(`timeout must be between 1 and 20 seconds, got ${this.timeout.toSeconds()}`);
1039+
throw new cdk.UnscopedValidationError(`timeout must be between 1 and 20 seconds, got ${this.timeout.toSeconds()}`);
10401040
}
10411041
}
10421042

@@ -1293,19 +1293,19 @@ export class Service extends cdk.Resource implements IService, iam.IGrantable {
12931293

12941294
if (this.source.codeRepository?.codeConfiguration.configurationSource == ConfigurationSourceType.REPOSITORY &&
12951295
this.source.codeRepository?.codeConfiguration.configurationValues) {
1296-
throw new Error('configurationValues cannot be provided if the ConfigurationSource is Repository');
1296+
throw new cdk.ValidationError('configurationValues cannot be provided if the ConfigurationSource is Repository', this);
12971297
}
12981298

12991299
if (props.serviceName !== undefined && !cdk.Token.isUnresolved(props.serviceName)) {
13001300
if (props.serviceName.length < 4 || props.serviceName.length > 40) {
1301-
throw new Error(
1302-
`\`serviceName\` must be between 4 and 40 characters, got: ${props.serviceName.length} characters.`,
1301+
throw new cdk.ValidationError(
1302+
`\`serviceName\` must be between 4 and 40 characters, got: ${props.serviceName.length} characters.`, this,
13031303
);
13041304
}
13051305

13061306
if (!/^[A-Za-z0-9][A-Za-z0-9\-_]*$/.test(props.serviceName)) {
1307-
throw new Error(
1308-
`\`serviceName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.serviceName}.`,
1307+
throw new cdk.ValidationError(
1308+
`\`serviceName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.serviceName}.`, this,
13091309
);
13101310
}
13111311
}
@@ -1384,7 +1384,7 @@ export class Service extends cdk.Resource implements IService, iam.IGrantable {
13841384
@MethodMetadata()
13851385
public addEnvironmentVariable(name: string, value: string) {
13861386
if (name.startsWith('AWSAPPRUNNER')) {
1387-
throw new Error(`Environment variable key ${name} with a prefix of AWSAPPRUNNER is not allowed`);
1387+
throw new cdk.ValidationError(`Environment variable key ${name} with a prefix of AWSAPPRUNNER is not allowed`, this);
13881388
}
13891389
this.variables.push({ name: name, value: value });
13901390
}
@@ -1395,7 +1395,7 @@ export class Service extends cdk.Resource implements IService, iam.IGrantable {
13951395
@MethodMetadata()
13961396
public addSecret(name: string, secret: Secret) {
13971397
if (name.startsWith('AWSAPPRUNNER')) {
1398-
throw new Error(`Environment secret key ${name} with a prefix of AWSAPPRUNNER is not allowed`);
1398+
throw new cdk.ValidationError(`Environment secret key ${name} with a prefix of AWSAPPRUNNER is not allowed`, this);
13991399
}
14001400
secret.grantRead(this.instanceRole);
14011401
this.secrets.push({ name: name, value: secret.arn });
@@ -1446,10 +1446,10 @@ export class Service extends cdk.Resource implements IService, iam.IGrantable {
14461446
];
14471447

14481448
if (codeEnv.every(el => el !== undefined) || imageEnv.every(el => el !== undefined)) {
1449-
throw new Error([
1449+
throw new cdk.ValidationError([
14501450
'You cannot set both \'environmentVariables\' and \'environment\' properties.',
14511451
'Please only use environmentVariables, as environment is deprecated.',
1452-
].join(' '));
1452+
].join(' '), this);
14531453
}
14541454

14551455
return codeEnv.find(el => el !== undefined) || imageEnv.find(el => el !== undefined) || {};

Diff for: packages/@aws-cdk/aws-apprunner-alpha/lib/vpc-connector.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ export class VpcConnector extends cdk.Resource implements IVpcConnector {
141141

142142
if (props.vpcConnectorName !== undefined && !cdk.Token.isUnresolved(props.vpcConnectorName)) {
143143
if (props.vpcConnectorName.length < 4 || props.vpcConnectorName.length > 40) {
144-
throw new Error(
145-
`\`vpcConnectorName\` must be between 4 and 40 characters, got: ${props.vpcConnectorName.length} characters.`,
144+
throw new cdk.ValidationError(
145+
`\`vpcConnectorName\` must be between 4 and 40 characters, got: ${props.vpcConnectorName.length} characters.`, this,
146146
);
147147
}
148148

149149
if (!/^[A-Za-z0-9][A-Za-z0-9\-_]*$/.test(props.vpcConnectorName)) {
150-
throw new Error(
151-
`\`vpcConnectorName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.vpcConnectorName}.`,
150+
throw new cdk.ValidationError(
151+
`\`vpcConnectorName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.vpcConnectorName}.`, this,
152152
);
153153
}
154154
}

Diff for: packages/@aws-cdk/aws-apprunner-alpha/lib/vpc-ingress-connection.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ export class VpcIngressConnection extends cdk.Resource implements IVpcIngressCon
148148

149149
if (props.vpcIngressConnectionName !== undefined && !cdk.Token.isUnresolved(props.vpcIngressConnectionName)) {
150150
if (props.vpcIngressConnectionName.length < 4 || props.vpcIngressConnectionName.length > 40) {
151-
throw new Error(
152-
`\`vpcIngressConnectionName\` must be between 4 and 40 characters, got: ${props.vpcIngressConnectionName.length} characters.`,
151+
throw new cdk.ValidationError(
152+
`\`vpcIngressConnectionName\` must be between 4 and 40 characters, got: ${props.vpcIngressConnectionName.length} characters.`, this,
153153
);
154154
}
155155

156156
if (!/^[A-Za-z0-9][A-Za-z0-9\-_]*$/.test(props.vpcIngressConnectionName)) {
157-
throw new Error(
158-
`\`vpcIngressConnectionName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.vpcIngressConnectionName}.`,
157+
throw new cdk.ValidationError(
158+
`\`vpcIngressConnectionName\` must start with an alphanumeric character and contain only alphanumeric characters, hyphens, or underscores after that, got: ${props.vpcIngressConnectionName}.`, this,
159159
);
160160
}
161161
}

Diff for: packages/aws-cdk-lib/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const enableNoThrowDefaultErrorIn = [
2828
'aws-applicationautoscaling',
2929
'aws-appsync',
3030
'aws-appmesh',
31+
'aws-apprunner',
3132
'aws-autoscaling',
3233
'aws-autoscaling-common',
3334
'aws-backup',

0 commit comments

Comments
 (0)