JS-DevTools / ono

Wrap errors without losing the original message, stack trace, or properties
https://jstools.dev/ono/
MIT License
106 stars 11 forks source link

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

Open ferm10n opened 3 years ago

ferm10n commented 3 years ago

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?