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' + }) +})