Skip to content

Commit 96f3acc

Browse files
kaizenccyashkh-amzn
authored andcommitted
feat(certificatemanager): throw ValidationErrors instead of untyped Errors (aws#33440)
### Issue Relates to aws#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 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 79197c3 commit 96f3acc

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
@@ -33,6 +33,7 @@ const enableNoThrowDefaultErrorIn = [
3333
'aws-backup',
3434
'aws-batch',
3535
'aws-chatbot',
36+
'aws-certificatemanager',
3637
'aws-cognito',
3738
'aws-cloudfront',
3839
'aws-cloudfront-origins',

packages/aws-cdk-lib/aws-certificatemanager/lib/certificate.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CfnCertificate } from './certificatemanager.generated';
44
import { apexDomain } from './util';
55
import * as cloudwatch from '../../aws-cloudwatch';
66
import * as route53 from '../../aws-route53';
7-
import { IResource, Token, Tags } from '../../core';
7+
import { IResource, Token, Tags, ValidationError } from '../../core';
88
import { addConstructMetadata } from '../../core/lib/metadata-resource';
99

1010
/**
@@ -287,7 +287,7 @@ export class Certificate extends CertificateBase implements ICertificate {
287287

288288
// check if domain name is 64 characters or less
289289
if (!Token.isUnresolved(props.domainName) && props.domainName.length > 64) {
290-
throw new Error('Domain name must be 64 characters or less');
290+
throw new ValidationError('Domain name must be 64 characters or less', this);
291291
}
292292

293293
const allDomainNames = [props.domainName].concat(props.subjectAlternativeNames || []);
@@ -300,7 +300,7 @@ export class Certificate extends CertificateBase implements ICertificate {
300300
const cert = new CfnCertificate(this, 'Resource', {
301301
domainName: props.domainName,
302302
subjectAlternativeNames: props.subjectAlternativeNames,
303-
domainValidationOptions: renderDomainValidation(validation, allDomainNames),
303+
domainValidationOptions: renderDomainValidation(this, validation, allDomainNames),
304304
validationMethod: validation.method,
305305
certificateTransparencyLoggingPreference,
306306
keyAlgorithm: props.keyAlgorithm?.name,
@@ -332,7 +332,7 @@ export enum ValidationMethod {
332332
}
333333

334334
// eslint-disable-next-line max-len
335-
function renderDomainValidation(validation: CertificateValidation, domainNames: string[]): CfnCertificate.DomainValidationOptionProperty[] | undefined {
335+
function renderDomainValidation(scope: Construct, validation: CertificateValidation, domainNames: string[]): CfnCertificate.DomainValidationOptionProperty[] | undefined {
336336
const domainValidation: CfnCertificate.DomainValidationOptionProperty[] = [];
337337

338338
switch (validation.method) {
@@ -348,13 +348,13 @@ function renderDomainValidation(validation: CertificateValidation, domainNames:
348348
for (const domainName of domainNames) {
349349
const validationDomain = validation.props.validationDomains?.[domainName];
350350
if (!validationDomain && Token.isUnresolved(domainName)) {
351-
throw new Error('When using Tokens for domain names, \'validationDomains\' needs to be supplied');
351+
throw new ValidationError('When using Tokens for domain names, \'validationDomains\' needs to be supplied', scope);
352352
}
353353
domainValidation.push({ domainName, validationDomain: validationDomain ?? apexDomain(domainName) });
354354
}
355355
break;
356356
default:
357-
throw new Error(`Unknown validation method ${validation.method}`);
357+
throw new ValidationError(`Unknown validation method ${validation.method}`, scope);
358358
}
359359

360360
return domainValidation.length !== 0 ? domainValidation : undefined;

packages/aws-cdk-lib/aws-certificatemanager/lib/dns-validated-certificate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class DnsValidatedCertificate extends CertificateBase implements ICertifi
9898
this.domainName = props.domainName;
9999
// check if domain name is 64 characters or less
100100
if (!Token.isUnresolved(props.domainName) && props.domainName.length > 64) {
101-
throw new Error('Domain name must be 64 characters or less');
101+
throw new cdk.ValidationError('Domain name must be 64 characters or less', this);
102102
}
103103
this.normalizedZoneName = props.hostedZone.zoneName;
104104
// Remove trailing `.` from zone name

0 commit comments

Comments
 (0)