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

Difference in error handling depending on argument position #334

Closed
Othey1991 opened this issue Nov 17, 2016 · 3 comments
Closed

Difference in error handling depending on argument position #334

Othey1991 opened this issue Nov 17, 2016 · 3 comments

Comments

@Othey1991
Copy link

Othey1991 commented Nov 17, 2016

Hello,

an error is handled differently depending on the position of the error argument.
When the error is on the first position only the stacktrace is shown.
In every other case the stacktrace and the attributes are shown.

I see 2 options to make this consistent

  • Add addition attributes to the coerce function and use coerce for every argument (more code, but will work in node <= 4)
  • Do not use coerce function (will make error output less useful for node <= 4)

Here is code and example output to clarify:

const debug = require('debug')('namespace');
const error = Object.assign(new Error('error'), {hello: 'word'});
debug(error); // Error as first argument. Additional attributes are not shown.
debug('', error); // Error as second argument. Additional attributes are shown.

Results in

  namespace Error: error
    at Object.<anonymous> (/private/tmp/error-testing.js:3:29)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:382:7)
    at startup (bootstrap_node.js:137:9)
    at bootstrap_node.js:497:3 +0ms
  namespace  { Error: error
    at Object.<anonymous> (/private/tmp/error-testing.js:3:29)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:382:7)
    at startup (bootstrap_node.js:137:9)
    at bootstrap_node.js:497:3 hello: 'word' } +2ms

This is different for different versions of node.
In node 4 the standard Error.prototype.toString() did not include the stacktrace. Newer versions seem to show the stacktrace.

Here is the output for different versions of node

$ docker run --env DEBUG=* --volume $(pwd):/volume --workdir /volume node:4-alpine node error-testing.js
Thu, 17 Nov 2016 11:01:02 GMT namespace Error: error
    at Object.<anonymous> (/volume/error-testing.js:3:29)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:990:3
Thu, 17 Nov 2016 11:01:02 GMT namespace  { [Error: error] hello: 'word' }
$ docker run --env DEBUG=* --volume $(pwd):/volume --workdir /volume node:6-alpine node error-testing.js
Thu, 17 Nov 2016 11:01:07 GMT namespace Error: error
    at Object.<anonymous> (/volume/error-testing.js:3:29)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
Thu, 17 Nov 2016 11:01:07 GMT namespace  { Error: error
    at Object.<anonymous> (/volume/error-testing.js:3:29)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3 hello: 'word' }
$ docker run --env DEBUG=* --volume $(pwd):/volume --workdir /volume node:7-alpine node error-testing.js
Thu, 17 Nov 2016 11:01:12 GMT namespace Error: error
    at Object.<anonymous> (/volume/error-testing.js:3:29)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3
Thu, 17 Nov 2016 11:01:12 GMT namespace  { Error: error
    at Object.<anonymous> (/volume/error-testing.js:3:29)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3 hello: 'word' }

If you decide on which way you want to go, I will happily provide a pull request.

@thebigredgeek
Copy link
Contributor

@Othey1991 I think this is intended behavior. If you pass an error, we infer that you want a trace. If you pass some text followed by an error, we infer that you want to print out an object.

@thebigredgeek
Copy link
Contributor

#370 < btw, seems like a solid case for V3 middleware API :). Feel free to jump into the conversation

@mren
Copy link

mren commented Dec 22, 2016

Hello and thanks for the feedback.

configurable formatters are a nice addition. Thanks for showing that this is a work in progress.
I will watch this closely.

I'm still not sure if I understand why this is intended behavior.

To be clear: I do not talk about the stacktraces. Traces are shown in all recent node versions.
I'm talking about additional attributes added to an error.

Me as a developer, why would I want to have different display behavior depending on the position of my error?
If I am logging an error I want to see a consistent display behavior (meaning traces and additional attributes) without thinking about the position of the error.

Currently log.debug('', error) is showing better information than log.debug(error), which is confusing.

It seems that the basic Error.prototype.toString() method of recent node versions (>=6) improved and is now displaying better information than the coerce method in https://github.com/visionmedia/debug/blob/280a64d788bbd143b076a141e30407c52a99824b/src/debug.js#L196-L199

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

No branches or pull requests

3 participants