pinojs/pino-std-serializers

Make use of `err.cause` when available

voxpelli opened this issue · 3 comments

What

As the Error Cause tc39 proposal has reached stage 4 and is accepted + part of Node.js since version 16.9.0 I think it would be great if these serializers started to use them.

Historical context

Also: For a long while before this proposal there was Joyent's VError module, which eg. was recommended in this Joyent article from +7 years ago and which was adopted by eg. Netflix who forked it as NError. VError also uses .cause but in their case it points to a method that returns the cause rather than the cause itself.

Related approaches

I myself created a module, pony-cause, which apart from providing a polyfill for Error Causes (ErrorWithCause) also provides a couple of helpers which supports official Error Causes as well as VError/NError causes, two which would be helpful here:

  • messageWithCauses – resolves the messages from all causes into a single message, using the style of VError which is: To join them together using the equivalent of .join(': ')

  • stackWithCauses – provides causes for the full chain, also similar to how VError does:

    Error: something really bad happened here
        at Object.<anonymous> (/examples/fullStack.js:5:12)
        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:968:3
    caused by: Error: something bad happened
        at Object.<anonymous> (/examples/fullStack.js:3:12)
        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:968:3
    

This approach follows the prior art of VError/NError and extends it to support the new Error Causes, making it a neat approach.

This also aligns with the support for VError style errors which bunyan has, but with the addition of supporting that format for new Error Causes as well.

Suggested approach

Either implement the functionality of my pony-cause module directly in this module or pull in my pony-cause module to:

Related discussions

Will/can I submit a PR?

Yes 👍

What are we looking to accomplish? Also, I'd rather just support standard err.cause = Error('boom') than try to detect miscellaneous implementations like VError/NError.

If the change to this module is minimal, +1 in doing it without deps I think this is likely. Otherwise add the dependency.

I added a quick PR using my module: #78

I can of course look into making it a self-hosted solution instead if preferable.