async-error-catcher
async-error-catcher copied to clipboard
Is this still required?
I see this seems to be handled default by express now. I see there is a try catch in the express code where if any error is thrown, it catches and call next(err) on it.
Layer.prototype.handle_request = function handle(req, res, next) {
var fn = this.handle;
if (fn.length > 3) {
// not a standard request handler
return next();
}
try {
fn(req, res, next);
} catch (err) {
next(err);
}
};
The above code is from "/functions/node_modules/express/lib/router/layer.js:95:5"
Can you please confirm?
@voxelbusters I'm having an issue where the above code would seem to indicate that this is not required, however, errors thrown in my routes are not being caught as you would expect. I'll update you here if I find anything interesting...
@voxelbusters I've found the answer for you.
If you use async
/await
then express doesn't catch the promise rejecting.
app.get('/test1', (req, res) => {
throw new Error('Express catches this error');
});
app.get('/test2', async (req, res) => { // <--- note the `async`
throw new Error('Express does NOT catch this error :(');
});
So yes, you still need this.
Do note however, if you transpile your Express code using a tool like Babel, I suspect that express would catch those errors normally. I'm not sure why you would transpile server code though 🤷♂