Skip to content

Difference in error handling depending on argument position #334

Closed
@Othey1991

Description

@Othey1991

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions