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

Make use of err.cause when available #76

Closed
voxpelli opened this issue Nov 14, 2021 · 3 comments · Fixed by #78
Closed

Make use of err.cause when available #76

voxpelli opened this issue Nov 14, 2021 · 3 comments · Fixed by #78

Comments

@voxpelli
Copy link
Contributor

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 👍

@jsumners
Copy link
Member

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.

@mcollina
Copy link
Member

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

@voxpelli
Copy link
Contributor Author

I added a quick PR using my module: #78

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

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

Successfully merging a pull request may close this issue.

3 participants