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 updating errorKey with default error serializer (#1593) #1604

Merged
merged 1 commit into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/tools.js
Expand Up @@ -20,6 +20,7 @@ const {
nestedKeySym,
formattersSym,
messageKeySym,
errorKeySym,
nestedKeyStrSym
} = require('./symbols')
const { isMainThread } = require('worker_threads')
Expand Down Expand Up @@ -97,6 +98,7 @@ function asJson (obj, msg, num, time) {
const serializers = this[serializersSym]
const formatters = this[formattersSym]
const messageKey = this[messageKeySym]
const errorKey = this[errorKeySym]
let data = this[lsCacheSym][num] + time

// we need the child bindings added to the output first so instance logged
Expand All @@ -112,7 +114,11 @@ function asJson (obj, msg, num, time) {
for (const key in obj) {
value = obj[key]
if (Object.prototype.hasOwnProperty.call(obj, key) && value !== undefined) {
value = serializers[key] ? serializers[key](value) : value
if (serializers[key]) {
value = serializers[key](value)
} else if (key === errorKey && serializers.err) {
value = serializers.err(value)
}

const stringifier = stringifiers[key] || wildcardStringifier

Expand Down
14 changes: 14 additions & 0 deletions test/errorKey.test.js
Expand Up @@ -18,3 +18,17 @@ test('set the errorKey with error serializer', async ({ equal, same }) => {
equal(o[errorKey].message, 'test')
equal(typeof o[errorKey].stack, 'string')
})

test('set the errorKey without error serializer', async ({ equal, same }) => {
const stream = sink()
const errorKey = 'error'
const instance = pino({
errorKey
}, stream)
instance.error(new ReferenceError('test'))
const o = await once(stream, 'data')
equal(typeof o[errorKey], 'object')
equal(o[errorKey].type, 'ReferenceError')
equal(o[errorKey].message, 'test')
equal(typeof o[errorKey].stack, 'string')
})