Skip to content

Commit

Permalink
feat(apigatewayv2): throw ValidationError instead of untyped errors (
Browse files Browse the repository at this point in the history
…#33082)

### Issue 

`aws-apigatewayv2-integrations` 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
- [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*
  • Loading branch information
HBobertz authored Jan 24, 2025
1 parent 5e0f16d commit 5377586
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ baseConfig.rules['import/no-extraneous-dependencies'] = [

// no-throw-default-error
const enableNoThrowDefaultErrorIn = [
'aws-apigatewayv2-integrations',
'aws-apigatewayv2-authorizers',
'aws-lambda',
'aws-rds',
'aws-s3',
Expand All @@ -25,7 +27,6 @@ const enableNoThrowDefaultErrorIn = [
'aws-ssmcontacts',
'aws-ssmincidents',
'aws-ssmquicksetup',
'aws-apigatewayv2-authorizers',
'aws-synthetics',
'aws-route53',
'aws-route53-patterns',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpPrivateIntegration } from './private/integration';
import { HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig } from '../../../aws-apigatewayv2';
import * as ec2 from '../../../aws-ec2';
import * as elbv2 from '../../../aws-elasticloadbalancingv2';
import { ValidationError } from '../../../core/lib/errors';

/**
* Properties to initialize `HttpAlbIntegration`.
Expand Down Expand Up @@ -33,7 +34,7 @@ export class HttpAlbIntegration extends HttpPrivateIntegration {
vpc = this.listener.loadBalancer.vpc;
}
if (!vpc) {
throw new Error('The vpcLink property must be specified when using an imported Application Listener.');
throw new ValidationError('The vpcLink property must be specified when using an imported Application Listener.', options.scope);
}

const vpcLink = this._configureVpcLink(options, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpPrivateIntegration } from './private/integration';
import { HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig } from '../../../aws-apigatewayv2';
import * as ec2 from '../../../aws-ec2';
import * as elbv2 from '../../../aws-elasticloadbalancingv2';
import { ValidationError } from '../../../core/lib/errors';

/**
* Properties to initialize `HttpNlbIntegration`.
Expand Down Expand Up @@ -33,7 +34,7 @@ export class HttpNlbIntegration extends HttpPrivateIntegration {
vpc = this.listener.loadBalancer.vpc;
}
if (!vpc) {
throw new Error('The vpcLink property must be specified when using an imported Network Listener.');
throw new ValidationError('The vpcLink property must be specified when using an imported Network Listener.', options.scope);
}

const vpcLink = this._configureVpcLink(options, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IVpcLink,
} from '../../../../aws-apigatewayv2';
import * as ec2 from '../../../../aws-ec2';
import { ValidationError } from '../../../../core/lib/errors';

/**
* Options required to use an existing vpcLink or configure a new one
Expand Down Expand Up @@ -51,7 +52,7 @@ export abstract class HttpPrivateIntegration extends HttpRouteIntegration {
let vpcLink = configOptions.vpcLink;
if (!vpcLink) {
if (!configOptions.vpc) {
throw new Error('One of vpcLink or vpc should be provided for private integration');
throw new ValidationError('One of vpcLink or vpc should be provided for private integration', bindOptions.scope);
}

vpcLink = bindOptions.route.httpApi.addVpcLink({ vpc: configOptions.vpc });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpPrivateIntegrationOptions } from './base-types';
import { HttpPrivateIntegration } from './private/integration';
import { HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig } from '../../../aws-apigatewayv2';
import * as servicediscovery from '../../../aws-servicediscovery';
import { ValidationError } from '../../../core/lib/errors';

/**
* Properties to initialize `HttpServiceDiscoveryIntegration`.
Expand All @@ -26,9 +27,9 @@ export class HttpServiceDiscoveryIntegration extends HttpPrivateIntegration {
super(id);
}

public bind(_options: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig {
public bind(options: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig {
if (!this.props.vpcLink) {
throw new Error('The vpcLink property is mandatory');
throw new ValidationError('The vpcLink property is mandatory', options.scope);
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as apigwv2 from '../../../aws-apigatewayv2';
import * as iam from '../../../aws-iam';
import * as sfn from '../../../aws-stepfunctions';
import { ValidationError } from '../../../core/lib/errors';

/**
* Properties to initialize `HttpStepFunctionsIntegration`.
Expand Down Expand Up @@ -51,13 +52,13 @@ export class HttpStepFunctionsIntegration extends apigwv2.HttpRouteIntegration {

public bind(options: apigwv2.HttpRouteIntegrationBindOptions): apigwv2.HttpRouteIntegrationConfig {
if (this.props.subtype && !this.props.subtype.startsWith('StepFunctions-')) {
throw new Error('Subtype must start with `STEPFUNCTIONS_`');
throw new ValidationError('Subtype must start with `STEPFUNCTIONS_`', options.scope);
}
if (
this.props.subtype === apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_START_SYNC_EXECUTION
&& this.props.stateMachine.stateMachineType === sfn.StateMachineType.STANDARD
) {
throw new Error('Cannot use subtype `STEPFUNCTIONS_START_SYNC_EXECUTION` with a standard type state machine');
throw new ValidationError('Cannot use subtype `STEPFUNCTIONS_START_SYNC_EXECUTION` with a standard type state machine', options.scope);
}

const invokeRole = new iam.Role(options.scope, 'InvokeRole', {
Expand Down

0 comments on commit 5377586

Please sign in to comment.