Skip to content

Commit

Permalink
fix msg precendence when sending a merging object with msg and err (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-volq committed Feb 22, 2023
1 parent eafbc90 commit 1b6f541
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function write (_obj, msg, num) {
}
} else {
obj = _obj
if (msg === undefined && _obj[errorKey]) {
if (msg === undefined && _obj.msg === undefined && _obj[errorKey]) {
msg = _obj[errorKey].message
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,23 @@ test('correctly adds msg on error when nestedKey is used', async ({ same }) => {
msg: 'msg message'
})
})

test('msg should take precedence over error message on mergingObject', async ({ same }) => {
const err = new Error('myerror')
const stream = sink()
const instance = pino(stream)
instance.error({ msg: 'my message', err })
const result = await once(stream, 'data')
delete result.time
same(result, {
pid,
hostname,
level: 50,
err: {
type: 'Error',
stack: err.stack,
message: err.message
},
msg: 'my message'
})
})

0 comments on commit 1b6f541

Please sign in to comment.