Skip to content

Commit 0787840

Browse files
feat(ecr-assets): throw ValidationErrors instead of untyped Errors (#33899)
### Issue # (if applicable) Relates to #32569 ### Reason for this change untyped Errors are not recommended ### Description of changes ValidationErrors 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 b3187b9 commit 0787840

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const enableNoThrowDefaultErrorIn = [
5050
'aws-docdb',
5151
'aws-dynamodb',
5252
'aws-ecr',
53+
'aws-ecr-assets',
5354
'aws-efs',
5455
'aws-elasticloadbalancing',
5556
'aws-elasticloadbalancingv2',

packages/aws-cdk-lib/aws-ecr-assets/lib/image-asset.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path';
33
import { Construct } from 'constructs';
44
import { FingerprintOptions, FollowMode, IAsset } from '../../assets';
55
import * as ecr from '../../aws-ecr';
6-
import { Annotations, AssetStaging, FeatureFlags, FileFingerprintOptions, IgnoreMode, Stack, SymlinkFollowMode, Token, Stage, CfnResource } from '../../core';
6+
import { Annotations, AssetStaging, FeatureFlags, FileFingerprintOptions, IgnoreMode, Stack, SymlinkFollowMode, Token, Stage, CfnResource, ValidationError, UnscopedValidationError } from '../../core';
77
import * as cxapi from '../../cx-api';
88

99
/**
@@ -435,14 +435,14 @@ export class DockerImageAsset extends Construct implements IAsset {
435435
// resolve full path
436436
const dir = path.resolve(props.directory);
437437
if (!fs.existsSync(dir)) {
438-
throw new Error(`Cannot find image directory at ${dir}`);
438+
throw new ValidationError(`Cannot find image directory at ${dir}`, this);
439439
}
440440

441441
// validate the docker file exists
442442
this.dockerfilePath = props.file || 'Dockerfile';
443443
const file = path.join(dir, this.dockerfilePath);
444444
if (!fs.existsSync(file)) {
445-
throw new Error(`Cannot find file at ${file}`);
445+
throw new ValidationError(`Cannot find file at ${file}`, this);
446446
}
447447

448448
const defaultIgnoreMode = FeatureFlags.of(this).isEnabled(cxapi.DOCKER_IGNORE_SUPPORT)
@@ -582,7 +582,7 @@ export class DockerImageAsset extends Construct implements IAsset {
582582
function validateProps(props: DockerImageAssetProps) {
583583
for (const [key, value] of Object.entries(props)) {
584584
if (Token.isUnresolved(value)) {
585-
throw new Error(`Cannot use Token as value of '${key}': this value is used before deployment starts`);
585+
throw new UnscopedValidationError(`Cannot use Token as value of '${key}': this value is used before deployment starts`);
586586
}
587587
}
588588

@@ -593,7 +593,7 @@ function validateProps(props: DockerImageAssetProps) {
593593
function validateBuildProps(buildPropName: string, buildProps?: { [key: string]: string }) {
594594
for (const [key, value] of Object.entries(buildProps || {})) {
595595
if (Token.isUnresolved(key) || Token.isUnresolved(value)) {
596-
throw new Error(`Cannot use tokens in keys or values of "${buildPropName}" since they are needed before deployment`);
596+
throw new UnscopedValidationError(`Cannot use tokens in keys or values of "${buildPropName}" since they are needed before deployment`);
597597
}
598598
}
599599
}

packages/aws-cdk-lib/aws-ecr-assets/lib/tarball-asset.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path';
33
import { Construct } from 'constructs';
44
import { IAsset } from '../../assets';
55
import * as ecr from '../../aws-ecr';
6-
import { AssetStaging, Stack, Stage } from '../../core';
6+
import { AssetStaging, Stack, Stage, ValidationError } from '../../core';
77

88
/**
99
* Options for TarballImageAsset
@@ -60,7 +60,7 @@ export class TarballImageAsset extends Construct implements IAsset {
6060
super(scope, id);
6161

6262
if (!fs.existsSync(props.tarballFile)) {
63-
throw new Error(`Cannot find file at ${props.tarballFile}`);
63+
throw new ValidationError(`Cannot find file at ${props.tarballFile}`, this);
6464
}
6565

6666
const stagedTarball = new AssetStaging(this, 'Staging', { sourcePath: props.tarballFile });

0 commit comments

Comments
 (0)