fix: construct errors are rendered in a messy way#37290
Merged
mergify[bot] merged 8 commits intomainfrom Mar 19, 2026
Merged
Conversation
In the past, Construct Errors (`ValidationError`,
`UnscopedValidationError`, `AssumptionError`) had a number of downsides:
- If the construct path was not set (even for an
`UnscopedValidationError`) the path would be rendered as ` in
[undefined]`.
- For multiline error messages, parts of the error would be repeated.
- (When used as a library) Errors would include the JavaScript line of
code throwing the error, but a production CDK build is minified so it
would just be giant line of noise.
In this PR:
- Render both the call stack and the construct path visually distinct.
- Render the construct path visually using a tree.
- Only render the construct path if available (avoid rendering
`undefined` for errors are not related to a construct)
- Don't render the actual contents of the source line, to avoid a huge
minified line.
- In order to improve the readability of the stack trace, we render Just
My Code (this is a Visual Code term): we hide stack traces belonging
to library code, focusing only on user code.
Examples:
BEFORE
```
/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/aws-s3/lib/bucket.ts:2266
throw new UnscopedValidationError('InvalidBucketNameValue', `Invalid S3 bucket name (value: ${bucketName})${EOL}${errors.join(EOL)}`);
^
InvalidBucketNameValue: Invalid S3 bucket name (value: &*&*$)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 0)
Bucket name must start with a lowercase character or number (offset: 0)
Bucket name must end with a lowercase character or number (offset: 4)
at path [undefined]
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 0)
Bucket name must start with a lowercase character or number (offset: 0)
Bucket name must end with a lowercase character or number (offset: 4)
at Bucket.validateBucketName (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/aws-s3/lib/bucket.ts:2266:13)
at new Bucket (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/aws-s3/lib/bucket.ts:2319:12)
at new Bucket (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/lib/prop-injectable.ts:40:7)
at Object.<anonymous> (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/test/integ.error.ts:10:22)
at Module._compile (node:internal/modules/cjs/loader:1761:14)
at Module.m._compile (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/node_modules/ts-node/src/index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1893:10)
at Object.require.extensions.<computed> [as .ts] (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1481:32)
at Module._load (node:internal/modules/cjs/loader:1300:12)
```
AFTER
```
InvalidBucketNameValue: Invalid S3 bucket name (value: &*&*$)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 0)
Bucket name must start with a lowercase character or number (offset: 0)
Bucket name must end with a lowercase character or number (offset: 4)
at validateBucketName (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/aws-s3/lib/bucket.ts:1003)
at new Bucket (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/aws-s3/lib/bucket.ts:1046)
at new Bucket (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/lib/prop-injectable.ts:31)
at <anonymous> (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/test/integ.error.ts:10)
...node internals, m._compile in ts-node, require.extensions.<computed> in ts-node...
```
BEFORE
```
/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/test/integ.error.ts:24
throw new cdk.UnscopedValidationError('Blaaah', 'Some error');
^
Blaaah: Some error
at path [undefined]
at Object.<anonymous> (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/test/integ.error.ts:24:7)
at Module._compile (node:internal/modules/cjs/loader:1761:14)
at Module.m._compile (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/node_modules/ts-node/src/index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1893:10)
at Object.require.extensions.<computed> [as .ts] (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1481:32)
at Module._load (node:internal/modules/cjs/loader:1300:12)
at TracingChannel.traceSync (node:diagnostics_channel:328:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
```
AFTER
```
Blaaah: Some error
at <anonymous> (/Users/huijbers/Workspaces/PublicCDK/aws-cdk4/packages/aws-cdk-lib/core/test/integ.error.ts:22)
...node internals, m._compile in ts-node, require.extensions.<computed> in ts-node...
```
aws-cdk-automation
previously requested changes
Mar 19, 2026
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
otaviomacedo
approved these changes
Mar 19, 2026
Contributor
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Contributor
Merge Queue Status
This pull request spent 4 hours 31 minutes 31 seconds in the queue, including 44 minutes 46 seconds running CI. Required conditions to merge
|
Contributor
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Contributor
|
Comments on closed issues and PRs are hard for our team to see. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the past, Construct Errors (
ValidationError,UnscopedValidationError,AssumptionError) had a number of downsides:UnscopedValidationError) the path would be rendered asat path [undefined].In this PR:
undefinedfor errors are not related to a construct)Examples (see 👀 for what to pay attention to)
Scoped error (in CDK repo)
BEFORE
AFTER
Unscoped error (in CDK repo)
BEFORE
AFTER
When using CDK as a library
AFTER
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license