Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(certificatemanager): throw ValidationErrors instead of untyped Errors #33440

Merged
merged 4 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const enableNoThrowDefaultErrorIn = [
'aws-backup',
'aws-batch',
'aws-chatbot',
'aws-certificatemanager',
'aws-cognito',
'aws-cloudfront',
'aws-cloudfront-origins',
Expand Down
12 changes: 6 additions & 6 deletions packages/aws-cdk-lib/aws-certificatemanager/lib/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CfnCertificate } from './certificatemanager.generated';
import { apexDomain } from './util';
import * as cloudwatch from '../../aws-cloudwatch';
import * as route53 from '../../aws-route53';
import { IResource, Token, Tags } from '../../core';
import { IResource, Token, Tags, ValidationError } from '../../core';
import { addConstructMetadata } from '../../core/lib/metadata-resource';

/**
Expand Down Expand Up @@ -287,7 +287,7 @@ export class Certificate extends CertificateBase implements ICertificate {

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

const allDomainNames = [props.domainName].concat(props.subjectAlternativeNames || []);
Expand All @@ -300,7 +300,7 @@ export class Certificate extends CertificateBase implements ICertificate {
const cert = new CfnCertificate(this, 'Resource', {
domainName: props.domainName,
subjectAlternativeNames: props.subjectAlternativeNames,
domainValidationOptions: renderDomainValidation(validation, allDomainNames),
domainValidationOptions: renderDomainValidation(this, validation, allDomainNames),
validationMethod: validation.method,
certificateTransparencyLoggingPreference,
keyAlgorithm: props.keyAlgorithm?.name,
Expand Down Expand Up @@ -332,7 +332,7 @@ export enum ValidationMethod {
}

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

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

return domainValidation.length !== 0 ? domainValidation : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class DnsValidatedCertificate extends CertificateBase implements ICertifi
this.domainName = props.domainName;
// check if domain name is 64 characters or less
if (!Token.isUnresolved(props.domainName) && props.domainName.length > 64) {
throw new Error('Domain name must be 64 characters or less');
throw new cdk.ValidationError('Domain name must be 64 characters or less', this);
}
this.normalizedZoneName = props.hostedZone.zoneName;
// Remove trailing `.` from zone name
Expand Down