I'm using Nodejs.
I have a promise which request after a few seconds. But my Middleware not catches the error, but uncaughtException does.
router.all('/incaseoferror', async(req, res, next) => {
const promise = await new Promise(function (resolve, reject) {
setTimeout(function () {
reject(new Error('this is a test err, the error-middleware NOT catch and it NOT OKAY'));
}, 3000);
});
// throw new Error('test error - the error-middleware catch it and that okay');
});
function clientErrorHandler(err, req, res, next) {
console.log('in clientErrorHandler', err);
//but not catch the promise error!
});
process.on('uncaughtException', function (error) {
// the error is catch here..
});
Here is the problem: Let's say I have login function from another library and it give me a promise. something has failed in the function, I cant catch the error in my error-middleware to give the user response that the request is failed.
- I dont want to add try/catch in every middleware. (route===middleware)
- I want use async/await
- Using uncaughtException not help me because I can't return response to the user in the route.
So what can I do? any ideas?