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.

Winston exception handler doesn't handle UnhandledPromiseRejectionWarning

See original GitHub issue

Please tell us about your environment:

“devDependencies”: { “@types/chai”: “^4.1.7”, “@types/chai-as-promised”: “^7.1.0”, “@types/mocha”: “^5.2.6”, “@types/mock-fs”: “^3.6.30”, “@types/node”: “^11.11.0”, “@types/sinon”: “^7.0.9”, “@typescript-eslint/eslint-plugin”: “^1.4.2”, “@typescript-eslint/parser”: “^1.4.2”, “chai”: “^4.2.0”, “chai-as-promised”: “^7.1.1”, “eslint”: “^5.15.1”, “jest”: “^24.4.0”, “mocha”: “^6.0.2”, “mock-fs”: “^4.8.0”, “sinon”: “^7.2.7”, “ts-mockito”: “^2.3.1”, “ts-node”: “^8.0.3”, “typemoq”: “^2.1.0”, “typescript”: “^3.3.3333” }, “dependencies”: { “@types/argparse”: “^1.0.36”, “@types/fs-extra”: “^5.0.5”, “@types/request-promise”: “^4.1.42”, “argparse”: “^1.0.10”, “fs-extra”: “^7.0.1”, “moment”: “^2.24.0”, “queue”: “^6.0.0”, “request”: “^2.88.0”, “request-promise”: “^4.2.4”, “slack”: “^11.0.2”, “winston”: “^3.2.1” }

What is the problem?

Throwing Error inside the async function is not handled by Winston exception handler, an error is not being logged so information is lost.

Other information

const _format = winston.format.printf((info) => `[${moment().format('YYYY/MM/DD hh:mm:ss')}] ${info.level}: ${info.message}`);
winston.createLogger({
        transports: [
          new winston.transports.File({ 
            filename: path.join(<string>EnvVars.get('ROOT_DIR'), 'logs', 'exceptions.log'), // valid path
            format: winston.format.combine(this._format),
            handleExceptions: true
          })
        ]
      })

(async function(){
    throw new Error('test') // not working, exceptions.log is empty
})()

(function(){
    throw new Error('test') // works
})()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
adomas000commented, Mar 18, 2019

I guess a workaround would be to use ->

process.on('unhandledRejection', (reason, promise) => {
        throw reason;
})

And from there Winston would pick up an exception and log it

1reaction
DABHcommented, Mar 19, 2019

We added a RejectionHandler https://github.com/winstonjs/winston/blob/master/lib/winston/rejection-handler.js a few releases back – I think using that will help with your issue! Might need both exception handler and rejection handler, depending on what kinds of issues you want to catch. Feel free to follow-up if that doesn’t solve your issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

winston logger includes handling uncaught promise rejections ...
The solution to this for me was to write the exception handlers like this: transports: [ new transports. File({ filename: "./logs/errors. log", ...
Read more >
this error originated either by throwing inside of an async ...
unhandledpromiserejection: this error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was...
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
When a JavaScript error is not properly handled, an uncaughtException is emitted. These suggest the programmer has made an error, and they ...
Read more >
Connect timeout and execution timeout in nodejs driver
toArray() } catch (error) { console.error(error) } finally { console. ... It does not have a direct relation to MongoClient#connect .
Read more >
winston-elasticsearch | Yarn - Package Manager
createLogger({ transports: [ esTransport ] }); // Compulsory error handling logger.on('error', (error) => { console.error('Error in logger caught', error); }); ...
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