Skip to content

Commit

Permalink
feat(amplify-alpha): throw ValidationError instead of untyped errors (
Browse files Browse the repository at this point in the history
#33141)

### Issue 

`aws-amplify-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*
  • Loading branch information
mrgrain authored Jan 24, 2025
1 parent 8b472fc commit a7cd9eb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
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
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ baseConfig.rules['import/no-extraneous-dependencies'] = [

// no-throw-default-error
const enableNoThrowDefaultErrorIn = [
'aws-amplify',
'aws-amplifyuibuilder',
'aws-apigatewayv2-authorizers',
'aws-apigatewayv2-integrations',
'aws-elasticloadbalancing',
'aws-elasticloadbalancingv2',
'aws-elasticloadbalancingv2-actions',
'aws-elasticloadbalancingv2-targets',
'aws-elasticloadbalancingv2-targets',
'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

0 comments on commit a7cd9eb

Please sign in to comment.