pinojs/pino

Promise catch directly calling error log results in "Cannot read properties of undefined" exception

Opened this issue · 1 comments

Unsure if this is purposeful, but I ran into this small issue while replacing some of my existing warning logs.

Test file:

$ cat test.mjs
import pino from 'pino'
const log = pino()
Promise.reject("FAIL").catch(log.error);

Pino Exception:

/tmp/pino/node_modules/pino/lib/tools.js:68
      if (typeof this[msgPrefixSym] === 'string' && msg !== undefined && msg !== null) {
                     ^

TypeError: Cannot read properties of undefined (reading 'Symbol(pino.msgPrefix)')
    at LOG (/tmp/pino/node_modules/pino/lib/tools.js:68:22)

To get it working, you must bind the correct context:

$ cat test.mjs
import pino from 'pino'
const log = pino()
Promise.reject("FAIL").catch(log.error.bind(log));

// OR

Promise.reject("FAIL").catch(e => log.error(e));

Expected result: pino should just log the error without throwing an exception. Thanks

This is expected. Back in the days, .bind() was very expensive, so we avoided it. We would need to check if it's still the case.