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

Code example in the docs of Error.captureStackTrace needs better wording #12289

Closed
jaymuthus opened this issue Apr 9, 2017 · 5 comments
Closed
Labels
doc Issues and PRs related to the documentations. errors Issues and PRs related to JavaScript errors originated in Node.js core. good first issue Issues that are suitable for first-time contributors.

Comments

@jaymuthus
Copy link

jaymuthus commented Apr 9, 2017

Ref: https://nodejs.org/dist/latest-v7.x/docs/api/errors.html#errors_error_capturestacktrace_targetobject_constructoropt

function MyError() {
  Error.captureStackTrace(this, MyError);
}

// Without passing MyError to captureStackTrace, the MyError
// frame would show up in the .stack property. By passing
// the constructor, we omit that frame and all frames above it.
new MyError().stack;

Result:

Error
    at Object.<anonymous> (/Users/mjayaraman/SVNFiles/node/course/error.js:8:11)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3
function MyError() {
  Error.captureStackTrace(this);
}

// Without passing MyError to captureStackTrace, the MyError
// frame would show up in the .stack property. By passing
// the constructor, we omit that frame and all frames above it.
new MyError().stack;
Error
    at new MyError (/Users/mjayaraman/SVNFiles/node/course/error.js:2:9)
    at Object.<anonymous> (/Users/mjayaraman/SVNFiles/node/course/error.js:8:11)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3

Meaning, it just omits that frame, if we pass the constructor and not the above frames.

// the constructor, we omit that frame and all frames above it. 

To be changed to

// the constructor, we omit that frame.

or

// the constructor, we omit that frame, and retain all frames above it.
  • Version:
  • Platform:
  • Subsystem:
@vsemozhetbyt vsemozhetbyt added doc Issues and PRs related to the documentations. errors Issues and PRs related to JavaScript errors originated in Node.js core. labels Apr 9, 2017
@refack
Copy link
Contributor

refack commented Apr 9, 2017

Meaning, it just omits that frame, if we pass the constructor and not the above frames.

Above, not below 😉.
But I agree it could be better worded.

@refack
Copy link
Contributor

refack commented Apr 9, 2017

Meaning, it just omits that frame, if we pass the constructor and not the above frames.

It actual meaning is that it omits the frames that are deeper than the call point.

function foo() {
  bar()
}

function bar() {
  baz()
}

function baz() {
  function MyError() {
    Error.captureStackTrace(this, bar)
  }
  var e = new MyError()
  console.log(e.stack)
}

foo()
> foo()
Error
    at foo (repl:3:1)
    at repl:2:1
    at REPLServer.defaultEval (repl.js:339:29)
    at bound (domain.js:280:14)
    at REPLServer.onLine (repl.js:536:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:191:7)
    at REPLServer.Interface._onLine (readline.js:241:10)
    at REPLServer.Interface._line (readline.js:590:8)
    at REPLServer.Interface._ttyWrite (readline.js:869:14)
undefined

@refack refack added the good first issue Issues that are suitable for first-time contributors. label Apr 9, 2017
@joyeecheung joyeecheung changed the title Need a correction in the document, I guess Code example in the docs of Error.captureStackTrace needs better wording Apr 10, 2017
@joyeecheung
Copy link
Member

Edited the title to be clearer about what this issue is about.

@arturgvieira-zz
Copy link

Hi, I made the change, please let me know if its good to go. Thanks

MylesBorins pushed a commit that referenced this issue Jul 17, 2017
Edit to the comment in the stack trace capture, highlighting the use of
the constructorOpt argument in errors.md

Fixes: #12289
PR-URL: #12962
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben.bridgewater@fintura.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. errors Issues and PRs related to JavaScript errors originated in Node.js core. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

No branches or pull requests

6 participants