GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: improve example for Error.captureStackTrace() by brodo · Pull Request #46886 · nodejs/node

Conversation

@brodo

@brodo brodo commented Feb 28, 2023

Copy link
Copy Markdown
Contributor

Change the MyError example so that instances of MyError are instanceof Error and also native errors when checked with util.types.isNativeError().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss reacted with eyes emoji
All reactions
  • 👀 1 reaction
@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to Node.js documentation. errors Issues and PRs related to JavaScript errors originating in Node.js core. labels Feb 28, 2023
BridgeAR
BridgeAR reviewed Feb 28, 2023
View reviewed changes

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I see why you suggest this. I am not certain if the example becomes more difficult to read though, especially as it has new implications as outlined in my comment.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread doc/api/errors.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@brodo

brodo commented Feb 28, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Should we maybe add a suggestion to use class MyError extends Error {}? Instances of this class will be instanceof Error and native errors. They will also not show the MyError constructor in the stack trace.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brodo

brodo commented Feb 28, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I see why you suggest this. I am not certain if the example becomes more difficult to read though, especially as it has new implications as outlined in my comment.

I understand the readability concern. Essentially it's a trade-off between having a 'copyable' example and a readable one. It would be interesting to find out if there are packages on npm that contain this error implementation...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

BridgeAR
BridgeAR reviewed Mar 1, 2023
View reviewed changes

@BridgeAR BridgeAR left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I am hesitant to change the documentation like that as we mix different APIs and errors should ideally not be created like that in the first place.

What about changing the example to show how to hide multiple call sites? E.g.,

function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

That way we create a "real" error and only change it's stack trace accordingly.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brodo and tonivj5 reacted with heart emoji
All reactions
  • ❤️ 2 reactions
Comment thread doc/api/errors.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/errors.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@brodo

brodo commented Mar 1, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw new Error();
}

I like this example a lot. It illustrates the usage of the function much better. But shouldn't the last line be throw error;?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jasnell
jasnell approved these changes Mar 2, 2023
View reviewed changes
@brodo

brodo commented Mar 14, 2023

Copy link
Copy Markdown
Contributor Author

Is there anything else I need to do to so this can get merged?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@debadree25

debadree25 commented Mar 16, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Could you rebase and update the first commit message, it seems the description exceeds the size limit of 72

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

debadree25
debadree25 approved these changes Mar 16, 2023
View reviewed changes

@debadree25 debadree25 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The example looks good, would also wait for @BridgeAR 's review to see if the changes address all his concerns.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brodo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@debadree25 debadree25 added the commit-queue-squash PRs the Commit Queue should land as one squashed commit. label Mar 16, 2023
@debadree25
debadree25 requested a review from BridgeAR March 16, 2023 19:17
@brodo @BridgeAR
doc: improve example for Error.captureStackTrace()
7d8b38f
Change the `MyError` example so that instances of `MyError`are
`instanceof Error` and also  native errors when checked with
`util.types.isNativeError()`.

Co-authored-by: Ruben Bridgewater <[email protected]>
@brodo
brodo force-pushed the capture-stack-trace-example-improvement branch from c4b05b3 to 7d8b38f Compare March 18, 2023 11:56
@brodo

brodo commented Mar 18, 2023

Copy link
Copy Markdown
Contributor Author

Could you rebase and update the first commit message, it seems the description exceeds the size limit of 72

I've squashed all the commits and added line breaks.

debadree25 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lpinca
lpinca approved these changes Mar 26, 2023
View reviewed changes
@lpinca lpinca added the commit-queue PRs queued for automated landing through the Commit Queue. label Mar 26, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Mar 26, 2023
@nodejs-github-bot
nodejs-github-bot merged commit 38e6ac7 into nodejs:main Mar 26, 2023
@nodejs-github-bot

nodejs-github-bot commented Mar 26, 2023

Copy link
Copy Markdown
Collaborator

Landed in 38e6ac7

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RafaelGSS RafaelGSS mentioned this pull request Apr 3, 2023
2023-04-18, Version 20.0.0 (Current) #47381
Merged
RafaelGSS pushed a commit that referenced this pull request Apr 5, 2023
@brodo @BridgeAR @RafaelGSS
doc: improve example for Error.captureStackTrace()
d34ed2e
Change the `MyError` example so that instances of `MyError`are
`instanceof Error` and also  native errors when checked with
`util.types.isNativeError()`.

Co-authored-by: Ruben Bridgewater <[email protected]>
PR-URL: #46886
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
@RafaelGSS RafaelGSS mentioned this pull request Apr 6, 2023
v19.9.0 proposal #47441
Merged
RafaelGSS pushed a commit that referenced this pull request Apr 6, 2023
@brodo @BridgeAR @RafaelGSS
doc: improve example for Error.captureStackTrace()
ab2f699
Change the `MyError` example so that instances of `MyError`are
`instanceof Error` and also  native errors when checked with
`util.types.isNativeError()`.

Co-authored-by: Ruben Bridgewater <[email protected]>
PR-URL: #46886
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
RafaelGSS pushed a commit that referenced this pull request Apr 7, 2023
@brodo @BridgeAR @RafaelGSS
doc: improve example for Error.captureStackTrace()
7124624
Change the `MyError` example so that instances of `MyError`are
`instanceof Error` and also  native errors when checked with
`util.types.isNativeError()`.

Co-authored-by: Ruben Bridgewater <[email protected]>
PR-URL: #46886
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
danielleadams pushed a commit that referenced this pull request Jul 6, 2023
@brodo @BridgeAR @danielleadams
doc: improve example for Error.captureStackTrace()
3a85794
Change the `MyError` example so that instances of `MyError`are
`instanceof Error` and also  native errors when checked with
`util.types.isNativeError()`.

Co-authored-by: Ruben Bridgewater <[email protected]>
PR-URL: #46886
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
@danielleadams danielleadams mentioned this pull request Jul 10, 2023
v18.17.0 release proposal #48694
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell approved these changes
@lpinca lpinca lpinca approved these changes
@BridgeAR BridgeAR Awaiting requested review from BridgeAR
+1 more reviewer
@debadree25 debadree25 debadree25 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

commit-queue-squash PRs the Commit Queue should land as one squashed commit. doc Issues and PRs related to Node.js documentation. errors Issues and PRs related to JavaScript errors originating in Node.js core.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@brodo @debadree25 @nodejs-github-bot @jasnell @lpinca @BridgeAR
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.