From 1b6f541adb919fe7383877394bd66c89b050a614 Mon Sep 17 00:00:00 2001 From: leon-volq <77416113+leon-volq@users.noreply.github.com> Date: Wed, 22 Feb 2023 11:21:48 -0600 Subject: [PATCH] fix msg precendence when sending a merging object with msg and err (#1654) --- lib/proto.js | 2 +- test/error.test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/proto.js b/lib/proto.js index 026d64498..d1dc3ff7f 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -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 } } diff --git a/test/error.test.js b/test/error.test.js index 0eb0b560c..1869c3909 100644 --- a/test/error.test.js +++ b/test/error.test.js @@ -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' + }) +})