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

unhandledRejection Error #1478

Closed
ravievontech opened this issue Sep 21, 2018 · 2 comments
Closed

unhandledRejection Error #1478

ravievontech opened this issue Sep 21, 2018 · 2 comments

Comments

@ravievontech
Copy link

ravievontech commented Sep 21, 2018

I am facing unhandledRejection exception with "winston-2.4.0". I used winston-elasticsearch with winston. But the timeout exception is not being handled by winston. Is it some bug with version 2.4.0. The error is not being returned in final callback of log. Instead it throws exception for timeout which is not being handled.

 const winston = require('winston');
  const ElasticSearch = require('winston-elasticsearch');
  const env = process.env.ENV_MODE || 'dev';

  const host = process.env.ELASTICSEARCH_HOST || '127.0.0.1';
  const port = process.env.ELASTICSEARCH_PORT || 9200;
  const username = process.env.ELASTICSEARCH_USERNAME || 'elastic';
  const password = process.env.ELASTICSEARCH_PASSWORD || 'changeme';
  const useHttps = process.env.ELASTICSEARCH_USEHTTPS || true;
  const elasticOpts = [
    {
      protocol: useHttps === true || useHttps === 'true' ? 'https' : 'http',
      host: host,
      auth: `${username}:${password}`,
      port: port
    }
  ];

  let indexPrefix = `${env}-${index}`;
  let newInstance = new winston.Logger({
    transports: [
    new ElasticSearch({
      handleExceptions: true,
      indexPrefix,
      host: host,
      port: port,
      clientOpts: {
          host: elasticOpts
      }
    })]
  });

  newInstance.emitErrs = false;
  newInstance.on('error', function (err) {
      console.log('Winston Error:', err);
  });
  newInstance.setLevels(winston.config.npm.levels);

  function log(msg, cb) {
    let { level, metadata, message, index } = msg;
    metadata = src_1.CommonUtils.formatMeta(metadata);
    index = index || 'logs';

    newInstance.log(level, message, metadata, function (err, data) {
        let date = new Date();
        let suffix = date.toISOString().split('T')[0].split('-').join('.');
        if (cb) {
          cb(err, {
            message: 'Success',
            messageBody: msg,
            index: `${env}-${index}-${suffix}`
          });
        } else {
          console.log('No callback provided to log function', 'error:=', err, 'message=Success', 'messageBody:=', msg);
        }
    });
  }
@DABH
Copy link
Contributor

DABH commented Sep 21, 2018

Idk if unhandled rejections are supported by winston 2.x. In winston 3.x there is currently a WIP PR #1462 to add support for catching unhandled rejections -- which makes me think they are not supported in 2.x either.

winston-elasticsearch does seem to have winston 3.x support so you could try using winston 3.x, but if it's truly an unhandled promise rejection rather than some other error, you might need to wait for that PR to land.

@DABH
Copy link
Contributor

DABH commented Sep 21, 2018

In fact, if it's truly an unhandled rejection, let's close this as a dup of #921 and use that issue to track the status (along with #1462 )

@DABH DABH closed this as completed Jan 3, 2019
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

2 participants