question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle Rejections functionality is not working

See original GitHub issue
  • winston version?
    • winston@2
    • 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(() => {});

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:10

github_iconTop GitHub Comments

13reactions
mrehanabbasicommented, May 5, 2020

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.

0reactions
quickdraw6906commented, Aug 31, 2021

Thanks so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Handle Rejection in Sales in 7 Steps (With Tips)
Rejection is important in businesses because it can help you identify improvement opportunities in your selling strategies. Handling rejection ...
Read more >
10 Do's and Don'ts when Dealing with Rejection
You are not to blame and rejection does not define you. 8) Do Understand It Wasn't Meant to Be. If it doesn't work...
Read more >
How to Deal with Rejection at Work: 9 Powerful Tactics
When it comes to dealing with rejection at work, I've learned why and how you can't let it get to your head too...
Read more >
6 ways to handle rejection at work (without losing it) - Fortune
No one is perfect and that's the beauty of it. Keep things in perspective and remember that you're much more than a project–or...
Read more >
Rejection and How to Handle It (for Teens) - Kids Health
It's not how I wanted things to work out. But everyone gets rejected — and I can try again." Think about what you're...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found