Skip to content

Commit 5ee092a

Browse files
authored
feat(globalaccelerator): throw ValidationErrors instead of untyped errors (#34426)
### Issue Relates to #32569 ### Reason for this change untyped Errors are not recommended ### Description of changes `ValidationError`s 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 5cfea39 commit 5ee092a

File tree

7 files changed

+10
-11
lines changed

7 files changed

+10
-11
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ baseConfig.rules['import/no-extraneous-dependencies'] = [
1717
baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error'];
1818
// not yet supported
1919
const noThrowDefaultErrorNotYetSupported = [
20-
'aws-globalaccelerator',
21-
'aws-globalaccelerator-endpoints',
2220
'aws-iam',
2321
'aws-lambda-destinations',
2422
'aws-lambda-event-sources',
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Token } from '../../core';
1+
import { IConstruct } from 'constructs';
2+
import { Token, ValidationError } from '../../core';
23

3-
export function validateWeight(x?: number) {
4+
export function validateWeight(scope: IConstruct, x?: number) {
45
if (x !== undefined && !Token.isUnresolved(x) && (x < 0 || x > 255)) {
5-
throw new Error(`'weight' must be between 0 and 255, got: ${x}`);
6+
throw new ValidationError(`'weight' must be between 0 and 255, got: ${x}`, scope);
67
}
78
}

packages/aws-cdk-lib/aws-globalaccelerator-endpoints/lib/alb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class ApplicationLoadBalancerEndpoint implements ga.IEndpoint {
3636
public readonly region?: string;
3737

3838
constructor(private readonly loadBalancer: elbv2.IApplicationLoadBalancer, private readonly options: ApplicationLoadBalancerEndpointOptions = {}) {
39-
validateWeight(options.weight);
39+
validateWeight(loadBalancer, options.weight);
4040
this.region = loadBalancer.env.region;
4141
}
4242

packages/aws-cdk-lib/aws-globalaccelerator-endpoints/lib/eip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class CfnEipEndpoint implements ga.IEndpoint {
2424
public readonly region?: string;
2525

2626
constructor(private readonly eip: ec2.CfnEIP, private readonly options: CfnEipEndpointProps = {}) {
27-
validateWeight(options.weight);
27+
validateWeight(eip, options.weight);
2828

2929
this.region = Stack.of(eip).region;
3030
}

packages/aws-cdk-lib/aws-globalaccelerator-endpoints/lib/instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class InstanceEndpoint implements ga.IEndpoint {
3636
public readonly region?: string;
3737

3838
constructor(private readonly instance: ec2.IInstance, private readonly options: InstanceEndpointProps = {}) {
39-
validateWeight(options.weight);
39+
validateWeight(instance, options.weight);
4040

4141
this.region = instance.env.region;
4242
}

packages/aws-cdk-lib/aws-globalaccelerator-endpoints/lib/nlb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class NetworkLoadBalancerEndpoint implements ga.IEndpoint {
3636
public readonly region?: string;
3737

3838
constructor(private readonly loadBalancer: elbv2.INetworkLoadBalancer, private readonly options: NetworkLoadBalancerEndpointProps = {}) {
39-
validateWeight(options.weight);
39+
validateWeight(loadBalancer, options.weight);
4040
this.region = loadBalancer.env.region;
4141
}
4242

packages/aws-cdk-lib/aws-globalaccelerator/lib/accelerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ export class Accelerator extends cdk.Resource implements IAccelerator {
225225

226226
private validateAcceleratorName(name?: string) {
227227
if (!cdk.Token.isUnresolved(name) && name !== undefined && (name.length < 1 || name.length > 64)) {
228-
throw new Error(`Invalid acceleratorName value ${name}, must have length between 1 and 64, got: ${name.length}`);
228+
throw new cdk.ValidationError(`Invalid acceleratorName value ${name}, must have length between 1 and 64, got: ${name.length}`, this);
229229
}
230230
}
231231

232232
private validateIpAddresses(ipAddresses?: string[]) {
233233
if (ipAddresses !== undefined && (ipAddresses.length < 1 || ipAddresses.length > 2)) {
234-
throw new Error(`Invalid ipAddresses value [${ipAddresses}], you can specify one or two addresses, got: ${ipAddresses.length}`);
234+
throw new cdk.ValidationError(`Invalid ipAddresses value [${ipAddresses}], you can specify one or two addresses, got: ${ipAddresses.length}`, this);
235235
}
236236
}
237237
}

0 commit comments

Comments
 (0)