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.

Middleware shouldn't be async

See original GitHub issue

Middleware created by this module is an async function. That is incorrect because connect framework doesn’t await middleware it runs so any exception within the middleware won’t be caught and propagated properly.

I don’t see a good way to fix it besides dropping support for async to functions.

I’ve noticed this problem while investigating error that can trigger on res.setHeader with URL with invalid characters (error TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rchlcommented, Aug 15, 2019

Per my last comment, async middleware is not really a problem. Problem is if some of the exceptions are not caught but after #52 all are handled so this is fixed.

1reaction
rchlcommented, Apr 17, 2019

Actually, now that I think about it, it’s not really a problem to have an async middleware as long as you are catching errors and passing to next() function.

You can add big try/catch on whole code or something cleaner like an async wrapper function:

function asyncMiddleware(endpointHandlerFunction) {
    return function(req, res, next) {
        Promise.resolve(endpointHandlerFunction(req, res, next)).catch(next);
    };
};

and then wrap original middleware function like:

return  asyncMiddleware(async function (req, res, next) {
   ...
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing Async/Await Middleware in Express
I noticed you have: req.render('post', { title, body }); shouldn't it be: res.render('post', { title, body }); ?
Read more >
Express middleware cannot trap errors thrown by async/await ...
I thought Express wrapped all middleware functions with try/catch, ... try { await fn(req, res, next); // shouldn't this trap the ...
Read more >
The Trouble With Middleware - Async - Django Forum
I have synchronous middleware adapting around asynchronous views just fine, but this does mean we waste a whole synchronous thread per async ......
Read more >
Common JS Async / Await Patterns That You Shouldn't Use
Your framework has a middleware to catch errors so it doesn't crash the app and allows you cleaner code. But you weren't aware...
Read more >
Why You Shouldn't Mix Promise.then() With Async/Await Syntax
Mixing Promise.then() with async/await syntax is a recipe for bugs. ... traced the issue to an Express middleware function with the following code....
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