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

fix: support logging errors that do not contain a stack #457

Merged
merged 1 commit into from
Jul 20, 2018

Conversation

disintegrator
Copy link
Contributor

Fixes #456

Custom errors may not have their stack property set.
Pino used to serialize undefined stack which created invalid JSON output

Given the following contrived example:

const logger = require('pino')();
const err = new Error();
delete err.stack;
logger.error(err);

Would output:

{"level":50,"time":1532087320790,"msg":"","pid":20631,"hostname":"dev.local","type":"Error","stack":undefined,"v":1}

This is invalid JSON due to the "stack":undefined at the end of that output line.

This PR adds a fix to avoid logging the stack if it is not set. So the above code now outputs:

{"level":50,"time":1532094347667,"msg":"","pid":25624,"hostname":"dev.local","type":"Error","v":1}

Custom errors may not have their stack property set.
Pino used to serialize undefined stack which created invalid JSON output
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

Copy link
Member

@davidmarkclements davidmarkclements left a comment

Choose a reason for hiding this comment

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

nice work

@mcollina mcollina merged commit 3a14789 into pinojs:master Jul 20, 2018
@jsumners
Copy link
Member

Since null is valid JSON, wouldn't the more appropriate fix be to serialize it as null if error.stack === undefined?

@mcollina
Copy link
Member

mcollina commented Jul 21, 2018 via email

@jsumners
Copy link
Member

See #382

@davidmarkclements
Copy link
Member

... I think we've made a mistake here.

For the current implementation, due to coercion, there are seven ways in which there may not be a stack

  • delete
  • null
  • undefined value
  • false
  • empty string
  • 0
  • NaN

Let's remove the silly ones and go to the ones where a user may be attempting something real:

  • delete
  • null
  • undefined value
  • false
  • empty string

What's the (likely) expectations in each case?

  • delete - I want to remove stack. In this case there should be no stack property
  • null - I want stack to be null (as per PrettyPrint fails when error's stack is null #382). In this case it should be null. That's a valid JSON property.
  • undefined value - I want stack to have the value undefined. This can't be done with JSON, in this case only we might consider making it an empty string. But we could also just allow it to be removed and educate anyone who doesn't know this part of the JSON spec.
  • false - I want to stack to be false. Then it should be false.
  • empty string - I want stack to be an empty string. This will work anyway, but with extra unnecessary steps.

I should have spent more time reviewing this. I'm not sure what to do with this in v4, but in v5 we need to be more thoughtful in how we approach this.

@jsumners
Copy link
Member

You understand the problem now :)

@github-actions
Copy link

github-actions bot commented Feb 6, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid error JSON logged if stack is undefined
4 participants