Winston: Handle Rejections functionality is not working

Created on 17 Jul 2019  路  6Comments  路  Source: winstonjs/winston

  • _winston version?_

    • [ ] winston@2

    • [x] winston@3

  • _node -v outputs:_ v10.16.0
  • _Operating System?_ macOS
  • _Language?_ ES6/7

What is the problem?

In both below cases nodejs will print default unhandled rejection error

1) setting handleRejections flag to true during transport creation is not working
2) calling logger.rejections.handle(transport) is not working

What do you expect to happen instead?

Winston should intercept unhandled rejection and log it

Other information

1st problem is due to not released change in winston-transport package: https://github.com/winstonjs/winston-transport/issues/47
2nd problem is due to typo in _addHandler method of lib/winston/rejection-handler.js: https://github.com/winstonjs/winston/blob/15c9653e4eeae1c76875603a722e11db9a6556bd/lib/winston/rejection-handler.js#L153

This line should be ADDED handler.handleRejections = true

There is one important thing: without handleExceptions, handleRejections won't log anything, due to below check: https://github.com/winstonjs/winston-transport/blob/46db8f3c8cd8b106ade8d7e04a191ee388683d60/index.js#L70

Testcases

Fails

const { createLogger, transports } = require('winston');

const logger = createLogger({
  transports: new transports.Console({
    level: 'info',
    handleRejections: true,
  }),
});

logger.info('Start');

new Promise((resolve, reject) => {
  process.nextTick(() => {
    reject(new Error('Rejected'));
  });
}).then(() => {});

Succeeds (Trick is in using Object.assign on transport instance)

const { createLogger, transports } = require('winston');

const logger = createLogger({
  transports: Object.assign(
    new transports.Console({
      handleExceptions: true,
    }),
    {
      handleRejections: true,
    },
  ),
});

logger.info('Start');

new Promise((resolve, reject) => {
  process.nextTick(() => {
    reject(new Error('Rejected'));
  });
}).then(() => {});

Most helpful comment

Any update on this? The documentation of winston@3 on github shows the rejections section but it is not available on the npm package page.

All 6 comments

Any progress on this?

Any update on this? The documentation of winston@3 on github shows the rejections section but it is not available on the npm package page.

I think this is fixed with #1779 and can be closed?

Probably, and winston-transport was released 2 months ago with required change too. I will re-test with latest version

Looks like issue is fixed, but only if handleExceptions is also set to true

Still not working

Was this page helpful?
0 / 5 - 0 ratings