-
Notifications
You must be signed in to change notification settings - Fork 33
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
errWithCause only serializes Error objects #158
Comments
That is true, but you should NOT do that. You should only throw instances of things that derive from |
I completely agree, but best practices do not always translate to reality. While we can enforce this in our own code, we have no way to enforce this in external npm libraries (or even in very old nodejs core code, potentially). Wrapping every single npm library in a try catch is not simply not feasible. The majority of errors that our projects deal with are thrown from npm libraries. Given that JS allows this - regardless of how insane it is - i think pino serialization should account for this also. |
Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
Here is a PR with some questions as comments |
We also need this because we do something like this:
Which is completely valid and useful. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause |
This is our use case too. |
@ifeltsweet @darcyrush The preferable way to do that would be to make a Then you can log it in a more desirable way And strictly speaking the If it's a HTTP code If it's a HTTP code Adding the |
For historical context: The main reason why Error causes can be any value is because all of So locking it down to be only See original discussion at tc39/proposal-error-cause#21 and also see es-shims/error-cause#2 |
Javascript can throw anything, not just Error objects. This includes primitives like strings and numbers;
The bundled lib.es2022.error.d.ts TypeScript typings indirectly hints at this;
As such, I believe this is unexpected;
I would expect the cause to be populated with
42
. Likewise I would expect it to be populated if a string was thrown, or whatever else is legal to be thrown in JavaScript, including objects that don't extend the JS Error class. Instead, the cause is simply lost.This example may seem like a contrived edge case, but I have come across libraries that throw their own "Error" objects, that do not extend from the native JS Error class,
The text was updated successfully, but these errors were encountered: