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

Output control for util.inspect / console.error ? #15

Open
ferm10n opened this issue Oct 12, 2020 · 0 comments
Open

Output control for util.inspect / console.error ? #15

ferm10n opened this issue Oct 12, 2020 · 0 comments

Comments

@ferm10n
Copy link

ferm10n commented Oct 12, 2020

I see that the toJSON is deliberately used for the Symbol.for('nodejs.util.inspect.custom') custom formatter. This doesn't feel very conducive to clear and concise error messages. Take the following for example and its output:

const ono = require("ono");

const onoError = ono({ foo: 'bar' }, 'Test Error');
console.log(onoError);

const ogError = new Error('Test Error');
ogError.foo = 'bar';
console.log(ogError);
{
  stack: 'Error: Test Error\n' +
    '    at Object.<anonymous> (/home/jsanders/ferm10n/dweb-mirror/demo.js:4:18)\n' +
    '    at Module._compile (internal/modules/cjs/loader.js:1138:30)\n' +
    '    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)\n' +
    '    at Module.load (internal/modules/cjs/loader.js:986:32)\n' +
    '    at Function.Module._load (internal/modules/cjs/loader.js:879:14)\n' +
    '    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)\n' +
    '    at internal/main/run_main_module.js:17:47',
  message: 'Test Error',
  toJSON: [Function: toJSON],
  foo: 'bar',
  name: 'Error',
  toString: [Function: toString]
}
Error: Test Error
    at Object.<anonymous> (/home/jsanders/ferm10n/dweb-mirror/demo.js:7:17)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  foo: 'bar'
}

Same information is conveyed, but the ono error is a lot more verbose. Is there a reason why toJSON was used for the inspect method? Would it make sense to only have toJSON only present enumerable properties, like how JSON.stringify serializes objects?

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

No branches or pull requests

1 participant