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(amplify-alpha): throw ValidationError instead of untyped errors #33141

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-amplify-alpha/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
baseConfig.rules['import/no-extraneous-dependencies'] = ['error', { devDependencies: true, peerDependencies: true } ];
baseConfig.rules['import/order'] = 'off';
baseConfig.rules['@aws-cdk/invalid-cfn-imports'] = 'off';
baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error'];
baseConfig.overrides.push({
files: ["./test/**"],
rules: {
"@cdklabs/no-throw-default-error": "off",
},
});

module.exports = baseConfig;
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Lazy, Resource, IResolvable, Token } from 'aws-cdk-lib/core';
import { Lazy, Resource, IResolvable, Token, ValidationError } from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import { CfnDomain } from 'aws-cdk-lib/aws-amplify';
import { IApp } from './app';
Expand Down Expand Up @@ -131,10 +131,10 @@ export class Domain extends Resource {

const domainName = props.domainName || id;
if (!Token.isUnresolved(domainName) && domainName.length > 255) {
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`);
throw new ValidationError(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`, this);
}
if (!Token.isUnresolved(domainName) && !/^(((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9])\.)+((?!-)[A-Za-z0-9-]{1,62}[A-Za-z0-9])(\.)?$/.test(domainName)) {
throw new Error(`Domain name must be a valid hostname, got: ${domainName}.`);
throw new ValidationError(`Domain name must be a valid hostname, got: ${domainName}.`, this);
}

const domain = new CfnDomain(this, 'Resource', {
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ baseConfig.rules['import/no-extraneous-dependencies'] = [
const enableNoThrowDefaultErrorIn = [
'aws-apigatewayv2-integrations',
'aws-apigatewayv2-authorizers',
'aws-amplify',
'aws-amplifyuibuilder',
'aws-lambda',
'aws-rds',
'aws-s3',
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/core/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ abstract class ConstructError extends Error {
*
* A ValidationError is always attached to a Construct scope. To a user, the error will present with additional
* information on the construct that caused the validation to fail.
*
* @internal
*/
export class ValidationError extends ConstructError {
public get type(): 'validation' {
Expand All @@ -162,6 +164,8 @@ export class ValidationError extends ConstructError {
*
* To a User, these errors still present themselves as a "ValidationError".
* However they do not contain any information about the location in the construct tree.
*
* @internal
*/
export class UnscopedValidationError extends ConstructError {
public get type(): 'validation' {
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export * from './expiration';
export * from './size';
export * from './stack-trace';
export { Element } from './deps';
export { Errors } from './errors';
export * from './errors';

export * from './app';
export * from './context-provider';
Expand Down
Loading