Skip to content

Commit 1e20df6

Browse files
authored
feat(glue): throw ValidationError instead of untyped errors (#35084)
### Issue # (if applicable) `aws-glue-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 - [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 1d35961 commit 1e20df6

File tree

14 files changed

+47
-33
lines changed

14 files changed

+47
-33
lines changed

packages/@aws-cdk/aws-glue-alpha/.eslintrc.js

Lines changed: 7 additions & 0 deletions
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;

packages/@aws-cdk/aws-glue-alpha/lib/code.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class AssetCode extends Code {
6666
super();
6767

6868
if (fs.lstatSync(this.path).isDirectory()) {
69-
throw new Error(`Code path ${this.path} is a directory. Only files are supported`);
69+
throw new cdk.UnscopedValidationError(`Code path ${this.path} is a directory. Only files are supported`);
7070
}
7171
}
7272

@@ -78,7 +78,7 @@ export class AssetCode extends Code {
7878
...this.options,
7979
});
8080
} else if (cdk.Stack.of(this.asset) !== cdk.Stack.of(scope)) {
81-
throw new Error(`Asset is already associated with another stack '${cdk.Stack.of(this.asset).stackName}'. ` +
81+
throw new cdk.UnscopedValidationError(`Asset is already associated with another stack '${cdk.Stack.of(this.asset).stackName}'. ` +
8282
'Create a new Code instance for every stack.');
8383
}
8484
this.asset.grantRead(grantable);

packages/@aws-cdk/aws-glue-alpha/lib/database.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArnFormat, IResource, Lazy, Names, Resource, Stack } from 'aws-cdk-lib/core';
1+
import { ArnFormat, IResource, Lazy, Names, Resource, Stack, UnscopedValidationError } from 'aws-cdk-lib/core';
22
import { Construct } from 'constructs';
33
import { CfnDatabase } from 'aws-cdk-lib/aws-glue';
44
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
@@ -152,12 +152,12 @@ export class Database extends Resource implements IDatabase {
152152

153153
function validateLocationUri(locationUri: string): void {
154154
if (locationUri.length < 1 || locationUri.length > 1024) {
155-
throw new Error(`locationUri length must be (inclusively) between 1 and 1024, got ${locationUri.length}`);
155+
throw new UnscopedValidationError(`locationUri length must be (inclusively) between 1 and 1024, got ${locationUri.length}`);
156156
}
157157
}
158158

159159
function validateDescription(description: string): void {
160160
if (description.length > 2048) {
161-
throw new Error(`description length must be less than or equal to 2048, got ${description.length}`);
161+
throw new UnscopedValidationError(`description length must be less than or equal to 2048, got ${description.length}`);
162162
}
163163
}

packages/@aws-cdk/aws-glue-alpha/lib/external-table.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Column } from './schema';
66
import { PartitionIndex, TableBase, TableBaseProps } from './table-base';
77
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
88
import { propertyInjectable } from 'aws-cdk-lib/core/lib/prop-injectable';
9+
import { ValidationError } from 'aws-cdk-lib';
910

1011
export interface ExternalTableProps extends TableBaseProps {
1112
/**
@@ -90,7 +91,7 @@ export class ExternalTable extends TableBase {
9091
},
9192
parameters: props.storageParameters ? props.storageParameters.reduce((acc, param) => {
9293
if (param.key in acc) {
93-
throw new Error(`Duplicate storage parameter key: ${param.key}`);
94+
throw new ValidationError(`Duplicate storage parameter key: ${param.key}`, this);
9495
}
9596
const key = param.key;
9697
acc[key] = param.value;

packages/@aws-cdk/aws-glue-alpha/lib/jobs/job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ export abstract class Job extends JobBase {
488488
const reservedArgs = new Set(['--debug', '--mode', '--JOB_NAME']);
489489
Object.keys(defaultArguments).forEach((arg) => {
490490
if (reservedArgs.has(arg)) {
491-
throw new Error(`The ${arg} argument is reserved by Glue. Don't set it`);
491+
throw new cdk.ValidationError(`The ${arg} argument is reserved by Glue. Don't set it`, this);
492492
}
493493
});
494494
}

packages/@aws-cdk/aws-glue-alpha/lib/jobs/ray-job.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Construct } from 'constructs';
55
import { JobType, GlueVersion, WorkerType, Runtime } from '../constants';
66
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
77
import { propertyInjectable } from 'aws-cdk-lib/core/lib/prop-injectable';
8+
import { ValidationError } from 'aws-cdk-lib/core';
89

910
/**
1011
* Properties for creating a Ray Glue job
@@ -77,7 +78,7 @@ export class RayJob extends Job {
7778
};
7879

7980
if (props.workerType && props.workerType !== WorkerType.Z_2X) {
80-
throw new Error('Ray jobs only support Z.2X worker type');
81+
throw new ValidationError('Ray jobs only support Z.2X worker type', this);
8182
}
8283

8384
const jobResource = new CfnJob(this, 'Resource', {

packages/@aws-cdk/aws-glue-alpha/lib/jobs/scala-spark-etl-job.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Code } from '../code';
55
import { SparkJob, SparkJobProps } from './spark-job';
66
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
77
import { propertyInjectable } from 'aws-cdk-lib/core/lib/prop-injectable';
8+
import { ValidationError } from 'aws-cdk-lib/core';
89

910
/**
1011
* Properties for creating a Scala Spark ETL job
@@ -89,7 +90,7 @@ export class ScalaSparkEtlJob extends SparkJob {
8990
};
9091

9192
if ((!props.workerType && props.numberOfWorkers !== undefined) || (props.workerType && props.numberOfWorkers === undefined)) {
92-
throw new Error('Both workerType and numberOfWorkers must be set');
93+
throw new ValidationError('Both workerType and numberOfWorkers must be set', this);
9394
}
9495

9596
const jobResource = new CfnJob(this, 'Resource', {

packages/@aws-cdk/aws-glue-alpha/lib/jobs/scala-spark-streaming-job.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Code } from '../code';
55
import { SparkJob, SparkJobProps } from './spark-job';
66
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
77
import { propertyInjectable } from 'aws-cdk-lib/core/lib/prop-injectable';
8+
import { ValidationError } from 'aws-cdk-lib/core';
89

910
/**
1011
* Properties for creating a Scala Spark ETL job
@@ -89,7 +90,7 @@ export class ScalaSparkStreamingJob extends SparkJob {
8990
};
9091

9192
if ((!props.workerType && props.numberOfWorkers !== undefined) || (props.workerType && props.numberOfWorkers === undefined)) {
92-
throw new Error('Both workerType and numberOfWorkers must be set');
93+
throw new ValidationError('Both workerType and numberOfWorkers must be set', this);
9394
}
9495

9596
const jobResource = new CfnJob(this, 'Resource', {

packages/@aws-cdk/aws-glue-alpha/lib/jobs/spark-job.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
44
import * as constructs from 'constructs';
55
import { Code } from '../code';
66
import { Job, JobProps } from './job';
7-
import { Token } from 'aws-cdk-lib';
7+
import { Token, UnscopedValidationError } from 'aws-cdk-lib';
88
import { EOL } from 'os';
99

1010
/**
@@ -198,7 +198,7 @@ function validateSparkUiPrefix(prefix?: string): void {
198198
}
199199

200200
if (errors.length > 0) {
201-
throw new Error(`Invalid prefix format (value: ${prefix})${EOL}${errors.join(EOL)}`);
201+
throw new UnscopedValidationError(`Invalid prefix format (value: ${prefix})${EOL}${errors.join(EOL)}`);
202202
}
203203
}
204204

packages/@aws-cdk/aws-glue-alpha/lib/s3-table.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Column } from './schema';
77
import { PartitionIndex, TableBase, TableBaseProps } from './table-base';
88
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
99
import { propertyInjectable } from 'aws-cdk-lib/core/lib/prop-injectable';
10+
import { UnscopedValidationError, ValidationError } from 'aws-cdk-lib';
1011

1112
/**
1213
* Encryption options for a Table.
@@ -162,7 +163,7 @@ export class S3Table extends TableBase {
162163
},
163164
parameters: props.storageParameters ? props.storageParameters.reduce((acc, param) => {
164165
if (param.key in acc) {
165-
throw new Error(`Duplicate storage parameter key: ${param.key}`);
166+
throw new ValidationError(`Duplicate storage parameter key: ${param.key}`, this);
166167
}
167168
const key = param.key;
168169
acc[key] = param.value;
@@ -264,7 +265,7 @@ function createBucket(table: S3Table, props: S3TableProps) {
264265
let bucket = props.bucket;
265266

266267
if (bucket && (props.encryption !== undefined && props.encryption !== TableEncryption.CLIENT_SIDE_KMS)) {
267-
throw new Error('you can not specify encryption settings if you also provide a bucket');
268+
throw new UnscopedValidationError('you can not specify encryption settings if you also provide a bucket');
268269
}
269270

270271
const encryption = props.encryption || TableEncryption.S3_MANAGED;

0 commit comments

Comments
 (0)