Skip to content

Commit dd34d2e

Browse files
authored
feat(apigatewayv2-authorizers): throw ValidationError instead of untyped errors (#33076)
### Issue `aws-apigatewayv2-authorizers` 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*
1 parent dc0cb8d commit dd34d2e

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const enableNoThrowDefaultErrorIn = [
2525
'aws-ssmcontacts',
2626
'aws-ssmincidents',
2727
'aws-ssmquicksetup',
28+
'aws-apigatewayv2-authorizers',
2829
'aws-synthetics',
2930
'aws-s3-assets',
3031
'aws-s3-deployment',

packages/aws-cdk-lib/aws-apigatewayv2-authorizers/lib/http/jwt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HttpRouteAuthorizerConfig,
66
IHttpRouteAuthorizer,
77
} from '../../../aws-apigatewayv2';
8+
import { UnscopedValidationError } from '../../../core/lib/errors';
89

910
/**
1011
* Properties to initialize HttpJwtAuthorizer.
@@ -59,7 +60,7 @@ export class HttpJwtAuthorizer implements IHttpRouteAuthorizer {
5960
*/
6061
public get authorizerId(): string {
6162
if (!this.authorizer) {
62-
throw new Error(
63+
throw new UnscopedValidationError(
6364
'Cannot access authorizerId until authorizer is attached to a HttpRoute',
6465
);
6566
}

packages/aws-cdk-lib/aws-apigatewayv2-authorizers/lib/http/lambda.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { ServicePrincipal } from '../../../aws-iam';
1111
import { IFunction } from '../../../aws-lambda';
1212
import { Stack, Duration, Names } from '../../../core';
13+
import { UnscopedValidationError, ValidationError } from '../../../core/lib/errors';
1314

1415
/**
1516
* Specifies the type responses the lambda returns
@@ -90,7 +91,7 @@ export class HttpLambdaAuthorizer implements IHttpRouteAuthorizer {
9091
*/
9192
public get authorizerId(): string {
9293
if (!this.authorizer) {
93-
throw new Error(
94+
throw new UnscopedValidationError(
9495
'Cannot access authorizerId until authorizer is attached to a HttpRoute',
9596
);
9697
}
@@ -99,7 +100,7 @@ export class HttpLambdaAuthorizer implements IHttpRouteAuthorizer {
99100

100101
public bind(options: HttpRouteAuthorizerBindOptions): HttpRouteAuthorizerConfig {
101102
if (this.httpApi && (this.httpApi.apiId !== options.route.httpApi.apiId)) {
102-
throw new Error('Cannot attach the same authorizer to multiple Apis');
103+
throw new ValidationError('Cannot attach the same authorizer to multiple Apis', options.scope);
103104
}
104105

105106
if (!this.authorizer) {

packages/aws-cdk-lib/aws-apigatewayv2-authorizers/lib/http/user-pool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { HttpAuthorizer, HttpAuthorizerType, HttpRouteAuthorizerBindOptions, HttpRouteAuthorizerConfig, IHttpRouteAuthorizer } from '../../../aws-apigatewayv2';
22
import { IUserPool, IUserPoolClient } from '../../../aws-cognito';
33
import { Stack } from '../../../core';
4+
import { UnscopedValidationError } from '../../../core/lib/errors';
45

56
/**
67
* Properties to initialize HttpUserPoolAuthorizer.
@@ -59,7 +60,7 @@ export class HttpUserPoolAuthorizer implements IHttpRouteAuthorizer {
5960
*/
6061
public get authorizerId(): string {
6162
if (!this.authorizer) {
62-
throw new Error(
63+
throw new UnscopedValidationError(
6364
'Cannot access authorizerId until authorizer is attached to a HttpRoute',
6465
);
6566
}

packages/aws-cdk-lib/aws-apigatewayv2-authorizers/lib/websocket/lambda.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { ServicePrincipal } from '../../../aws-iam';
1111
import { IFunction } from '../../../aws-lambda';
1212
import { Stack, Names } from '../../../core';
13+
import { ValidationError } from '../../../core/lib/errors';
1314

1415
/**
1516
* Properties to initialize WebSocketTokenAuthorizer.
@@ -49,7 +50,7 @@ export class WebSocketLambdaAuthorizer implements IWebSocketRouteAuthorizer {
4950

5051
public bind(options: WebSocketRouteAuthorizerBindOptions): WebSocketRouteAuthorizerConfig {
5152
if (this.webSocketApi && (this.webSocketApi.apiId !== options.route.webSocketApi.apiId)) {
52-
throw new Error('Cannot attach the same authorizer to multiple Apis');
53+
throw new ValidationError('Cannot attach the same authorizer to multiple Apis', options.scope);
5354
}
5455

5556
if (!this.authorizer) {

0 commit comments

Comments
 (0)