Skip to content

fix(@angular/build): preserve error stack traces during prerendering#32872

Merged
clydin merged 1 commit intoangular:mainfrom
maruthang:fix-32503-prerender-error-stacks
Mar 27, 2026
Merged

fix(@angular/build): preserve error stack traces during prerendering#32872
clydin merged 1 commit intoangular:mainfrom
maruthang:fix-32503-prerender-error-stacks

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

When prerendering fails, the error output shows only the error message but no stack trace, making debugging very difficult. The nullish coalescing chain err.message ?? err.stack ?? err always resolves to err.message (since it's almost always defined on Error objects), so err.stack is never reached.

Issue Number: #32503

What is the new behavior?

  • Reorder the chain to err.stack ?? err.message ?? err.code ?? err at all three error-handling locations in prerender.ts, so the full stack trace is preserved when available.
  • Add assertIsError(err) for type safety, consistent with the existing pattern at line 370.
  • Include err.code in all three locations for consistency.

Does this PR introduce a breaking change?

  • Yes
  • No

@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 27, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@maruthang
Copy link
Copy Markdown
Contributor Author

@googlebot I signed it!

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves error reporting in the prerendering process by adding error assertions and reordering error property access to prioritize stack traces. The review feedback suggests using the logical OR operator (||) instead of nullish coalescing (??) when constructing error messages to ensure that empty strings for stack or message properties do not suppress more descriptive error details.

return {
errors: [`An error occurred while extracting routes.\n\n${err.message ?? err.stack ?? err}`],
errors: [
`An error occurred while extracting routes.\n\n${err.stack ?? err.message ?? err.code ?? err}`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using the logical OR operator (||) here instead of nullish coalescing (??). While ?? handles null and undefined, it doesn't handle empty strings. Properties like err.stack or err.message can be empty strings, which would cause this expression to resolve to an empty string, hiding error details. Using || would be more robust as it treats empty strings as falsy and falls back to the next property, ensuring a more descriptive error is always displayed.

Suggested change
`An error occurred while extracting routes.\n\n${err.stack ?? err.message ?? err.code ?? err}`,
`An error occurred while extracting routes.\n\n${err.stack || err.message || err.code || err}`

assertIsError(err);
errors.push(
`An error occurred while prerendering route '${route}'.\n\n${err.message ?? err.stack ?? err.code ?? err}`,
`An error occurred while prerendering route '${route}'.\n\n${err.stack ?? err.message ?? err.code ?? err}`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using the logical OR operator (||) here instead of nullish coalescing (??). While ?? handles null and undefined, it doesn't handle empty strings. Properties like err.stack or err.message can be empty strings, which would cause this expression to resolve to an empty string, hiding error details. Using || would be more robust as it treats empty strings as falsy and falls back to the next property, ensuring a more descriptive error is always displayed.

Suggested change
`An error occurred while prerendering route '${route}'.\n\n${err.stack ?? err.message ?? err.code ?? err}`,
`An error occurred while prerendering route '${route}'.\n\n${err.stack || err.message || err.code || err}`

return {
errors: [
`An error occurred while extracting routes.\n\n${err.message ?? err.stack ?? err.code ?? err}`,
`An error occurred while extracting routes.\n\n${err.stack ?? err.message ?? err.code ?? err}`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using the logical OR operator (||) here instead of nullish coalescing (??). While ?? handles null and undefined, it doesn't handle empty strings. Properties like err.stack or err.message can be empty strings, which would cause this expression to resolve to an empty string, hiding error details. Using || would be more robust as it treats empty strings as falsy and falls back to the next property, ensuring a more descriptive error is always displayed.

Suggested change
`An error occurred while extracting routes.\n\n${err.stack ?? err.message ?? err.code ?? err}`,
`An error occurred while extracting routes.\n\n${err.stack || err.message || err.code || err}`

Reorder the nullish coalescing chain from `err.message ?? err.stack` to
`err.stack ?? err.message` so that the full stack trace is preserved when
available. Since `err.message` is almost always defined on Error objects,
the previous order meant `err.stack` was never reached.

Also add `assertIsError(err)` and consistent `err.code` inclusion across
all three error-handling locations for improved type safety and debugging.

Fixes angular#32503
@maruthang maruthang force-pushed the fix-32503-prerender-error-stacks branch from f4e029d to 6352ad5 Compare March 27, 2026 11:10
@alan-agius4 alan-agius4 linked an issue Mar 27, 2026 that may be closed by this pull request
@alan-agius4 alan-agius4 added the target: patch This PR is targeted for the next patch release label Mar 27, 2026
Copy link
Copy Markdown
Collaborator

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Mar 27, 2026
@clydin clydin merged commit 81e4faa into angular:main Mar 27, 2026
37 checks passed
@clydin
Copy link
Copy Markdown
Member

clydin commented Mar 27, 2026

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't swallow errors during prerender build

3 participants