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.

Context lost in an unhandled exception (or rejection)

See original GitHub issue

First of all - great module!

I was wondering if there’s any workaround to get the context, in case of an unhandled exception (or rejection, in case of promises). Here’s a sample code:

// cls.js
const cls = require('cls-hooked');
const clsNamespace = cls.createNamespace(process.getuid());

module.exports = {
  middleware: (req, res, next) => {
    run(() => {
      let someContextId = getTheIDSomehow();
      set('context-id', someContextId)
      next()
    }),
  ns: clsNamespace
}
// some-other-file.js
const ns = require('./cls').ns;

process.on('unhandledRejection', () => {
  // this will be undefined
  console.log(ns.get('context-id')); 
});

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
wesleytoddcommented, Nov 28, 2019

I would strongly recommend against this module (express-async-errors). Reaching deeply into the router like this is a bad idea, it will also not work in v5. The behavior you want is already added in v5 (https://github.com/pillarjs/router/pull/47 & https://github.com/pillarjs/router/commit/d2bce0c09d424ea89e77cef7743e12f411ed94f7).

In express v4, a much better approach is to use something like https://github.com/express-promise-router/express-promise-router

All that said, this is a bit off topic for this issue which is specifically about usage of this module.

0reactions
zhujun24commented, Apr 22, 2020

I have write a tidy npm package http-request-context to do this, using async_hooks, u can try it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

undefined is not an object (evalutating '_context.t0.response ...
I keep getting an Unhandled Promise Rejection: TypeError ...
Read more >
Tracking unhandled rejected Promises - 2ality
Let's look at how you can track unhandled rejections in browsers and in Node. js.
Read more >
A Definitive Guide to Handling Errors in JavaScript - Kinsta
This error occurs when you try to invoke a method that doesn't exist in your script, or it does but can not be...
Read more >
Frequently Asked Questions - Jasmine Documentation
I'm getting an unhandled promise rejection error but I think it's a false positive. ... Jasmine creates a user context and passes it...
Read more >
Errors - discord.js Guide
This error originates from the client attempting to execute an action that requires the token but the token not being available. This is...
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