pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

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

Open flyweight opened 5 months ago

flyweight commented 5 months ago

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

mcollina commented 5 months ago

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.