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

Serialize errors with cause #105

Merged
merged 4 commits into from
Jun 10, 2022

Conversation

hypesystem
Copy link
Contributor

Related to #76 and #78 which add serialization of cause into error messages.

This PR fixes #94 by making sure the chain of causes found in err.cause is serialized in full, bringing the final serialized result closer to what is printed by other serializers (e.g. those in the Node.js CLI and browser consoles).

The error serializer already handles the case where an error is not of type Error (or AggregateError) well, so there is no reason to add special handling for that. We can simply pass whatever is in `cause` into the serializer, and we should get a serialized result out.
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

mcollina commented Jun 9, 2022

@voxpelli could you take a look?

@mcollina
Copy link
Member

mcollina commented Jun 9, 2022

CI is failing

@hypesystem
Copy link
Contributor Author

hypesystem commented Jun 10, 2022

It seems that my assumption (comment) "message serialization already walks cause-chain" is only true in Node.js 16 for some reason? (I am running 16 locally, too, so it might be...) https://github.com/pinojs/pino-std-serializers/pull/105/files#diff-a6ff895a4ce70729791324c8118f43cffada5a9e9cfe42ea1df4a18707f2c430R207

This is odd to me, because it looks like the same functionality should be happening (cf. what was done in #76). Could it be that earlier Node.js versions don't support the cause key in options?

@hypesystem
Copy link
Contributor Author

Oh yes, looking at it now what fails is the following lines:

t.equal(serialized.message, 'bar: foo') // message serialization already walks cause-chain
t.ok(serialized.cause)
t.equal(serialized.cause.type, 'Error')

I'll try to look at other tests to see if similar things are handled there.

@hypesystem
Copy link
Contributor Author

In the other tests (of cause serialization into the error message) the .cause field is set manually on the Error, instead of using the options object: https://github.com/pinojs/pino-std-serializers/blob/master/test/err.test.js#L51-L53

This could be an approach, of course, but it kind of obscures what the case we are trying to support is... Open to input on how to approach this 😄

@mcollina
Copy link
Member

That's totally ok, follow the other tests.

@hypesystem
Copy link
Contributor Author

Fixed 😄

lib/err.js Show resolved Hide resolved
@hypesystem
Copy link
Contributor Author

hypesystem commented Jun 10, 2022

Hmm this comment on recursion by @jsumners made me realize that in #78 there was added some more checks, so it only recurses if _err.cause is an Error, and also has support for traversing the case where _err.cause is a function (for VError, which I'm not quite sure what is). C.f. https://github.com/pinojs/pino-std-serializers/blob/master/lib/err-helpers.js#L20-L31

Should I support that here too?

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@mcollina mcollina merged commit ae83956 into pinojs:master Jun 10, 2022
@hypesystem hypesystem mentioned this pull request Jun 10, 2022
@voxpelli
Copy link
Contributor

Um, so this now means that apart from the .stack getting all of the cause stacks appended and .message getting all of the cause messages appended we're now also doing that for each cause in itself?

So we're basically having two systems for serializing the causes at once?

I would say that either we walk the cause chain as this PR did, or we summarize the cause chain through the .stack and the .message as I did in #78.

Never do the both at once and especially never do the both recursively?

Sorry for not responding earlier :/

@hypesystem
Copy link
Contributor Author

@voxpelli I agree, and raised the issue here, too: #94 (comment)

@voxpelli
Copy link
Contributor

And in https://github.com/pinojs/pino-std-serializers/pull/78/files#diff-71c187d84f435004e68161a25e470d823f4de310a1d39b531cc02a90f3730149R56 we specifically opted out causes from recursion:

if (val instanceof Error && key !== 'cause') {
  /* eslint-disable no-prototype-builtins */
  if (!val.hasOwnProperty(seen)) {

I think it should be rewritten as:

if (val instanceof Error) {
  /* eslint-disable no-prototype-builtins */
  if (key !== 'cause' && !val.hasOwnProperty(seen)) {

That way it will exclude all Error causes from serialization, as intended, but keeping other causes

voxpelli added a commit to voxpelli/pino-std-serializers that referenced this pull request Jun 13, 2022
voxpelli added a commit to voxpelli/pino-std-serializers that referenced this pull request Jun 13, 2022
Instead rely on its content being appended to the message and the stack.

This is an alternative fix to pinojs#94, replacing pinojs#105 and pinojs#108 + makes pinojs#109 not needed
@hypesystem
Copy link
Contributor Author

I'm not sure I understand the explicit opting out of recursing over Errors? Could you elaborate on that @voxpelli

@voxpelli
Copy link
Contributor

Let's discuss it in #110

mcollina pushed a commit that referenced this pull request Jun 13, 2022
…ead (#110)

* Revert "Nested VError causes (#108)"

This reverts commit 5b88f7e.

* Revert "Serialize errors with cause (#105)"

This reverts commit ae83956.

* Never serialize `.cause` when an `Error`

Instead rely on its content being appended to the message and the stack.

This is an alternative fix to #94, replacing #105 and #108 + makes #109 not needed

* Add tests

* Add explainer comment
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 this pull request may close these issues.

Explicitly check cause property on Error instances
4 participants