Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Rejections functionality is not working #1673

Open
1 of 2 tasks
betalb opened this issue Jul 17, 2019 · 10 comments
Open
1 of 2 tasks

Handle Rejections functionality is not working #1673

betalb opened this issue Jul 17, 2019 · 10 comments

Comments

@betalb
Copy link

betalb commented Jul 17, 2019

  • 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: winstonjs/winston-transport#47
2nd problem is due to typo in _addHandler method of lib/winston/rejection-handler.js:

handler.handleExceptions = true;

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(() => {});
@mitjans
Copy link

mitjans commented Jan 28, 2020

Any progress on this?

@mrehanabbasi
Copy link

mrehanabbasi commented 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.

@ab-pm
Copy link

ab-pm commented Aug 12, 2020

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

@betalb
Copy link
Author

betalb commented Aug 12, 2020

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

@betalb
Copy link
Author

betalb commented Nov 20, 2020

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

@halfalicious
Copy link

Still not working

@rizwan-hanif
Copy link

it looks like on winston@3.3.3 setting handleRejections on transport will write it on handleExceptions transport.
e.g. following will write promise rejection logs to ex.logs

winston.exceptions.handle(
  new winston.transports.File({ filename: "ex.logs" })
);
winston.add(
  new winston.transports.File({
    filename: "logging.log",
    handleRejections: true,
  })
);

and following will write promise rejections along with exceptions to same transport logging.log

winston.add(
 new winston.transports.File({
   filename: "logging.log",
   handleRejections: true,
   handleExceptions: true
 })
);

@quickdraw6906
Copy link

quickdraw6906 commented Aug 30, 2021

Sadly, the neat workaround offered by @rizwan-hanif does not work for transports.Console

let defaultTransport =
  new LOG.transports.Console({
    level: logLevel,
    handleExceptions: true,
    handleRejections: true,
  });

Argument of type '{ level: string; handleExceptions: true; handleRejections: boolean; }' is not assignable to parameter of type 'ConsoleTransportOptions'.
Object literal may only specify known properties, but 'handleRejections' does not exist in type 'ConsoleTransportOptions'. Did you mean to write 'handleExceptions'?

@rizwan-hanif
Copy link

@quickdraw6906 issue is of winston-transport TypeScript types syncing reported here 44.
for TypeScript you might have to get latest winston-transport version with above issue resolved (unfortunately not available yet) or you can try this

let consoleTransportOptions={
    level: 'info',
    handleExceptions: true,
    handleRejections: true,
  }
let defaultTransport =
  new LOG.transports.Console(consoleTransportOptions);

@quickdraw6906
Copy link

Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants