Context lost in an unhandled exception (or rejection)
See original GitHub issueFirst 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:
- Created 4 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
I have write a tidy npm package http-request-context to do this, using async_hooks, u can try it.