Skip to content

Commit

Permalink
fix: update errorKey with default error serializer (#1593) (#1604)
Browse files Browse the repository at this point in the history
Co-authored-by: zahar517 <zahar.d517@gmail.com>
  • Loading branch information
zahar517 and zahar517 committed Dec 14, 2022
1 parent 1ff0976 commit 869f7bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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')
})

0 comments on commit 869f7bb

Please sign in to comment.