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.

Express should throw an error if `next()` is called without any more middleware in the stack

See original GitHub issue

I’ve spent half an hour trying to debug why the client was getting a 404 response back, and it turns out the cause was because we didn’t have a middleware in place to close the request (e.g. res.end()). In other words, the last middleware in the stack called next() but there wasn’t any more middleware to handle the request, so Express sent a 404 response.

I don’t think this should be the default behaviour, as this is clearly a case of programmer error. As such, I think express should throw an error instead, so that we can be made aware of the issue and add correct middleware in place or otherwise handle the error.

Silently responding with a 404 makes a developer hunt their own middleware stack first to see if and why they are terminating the request with a 404, which they aren’t.

Thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
dougwilsoncommented, Feb 24, 2017

It is not the responsibility of the developer to 404 themselves; that’s an implicit operation of nothing handling the request. This enables transparent mounting of apps without them needing to be made to be mountable, since a 404 is just a fallthough, so it makes it really easy to compose multiple apps together with before before after after behavior, just like express.static. For example, consider the following:

var app = express()
app.use('/api', legacyApi)
app.use('/api', newApi)
// ... etc

That would be impossible if the legacyApi threw an error and didn’t let 404s fall down.

2reactions
adamreisnzcommented, Feb 24, 2017

Isn’t it the responsibility of the app developers to throw a (custom) 404 error if needed? I would think that’s not the responsibility of Express, and that Express should warn developers about it instead, or perhaps return the 404 if you’ve configured Express to do so explicitly.

Currently, this implicit 404 was causing me to scratch my head a lot of times, because we issue manual 404’s in specific cases, and I was spending a lot of time analysing them before realising it was Express sending the 404’s, not our app.

In your second example app, instead of return next() one could simply do return next(new NotFoundError('I dont handle this')) to be more explicit, which is how we approach it most of the time. I guess I just don’t like auto-magic behaviour as it’s often hard to track.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling - Express.js
Express comes with a built-in error handler that takes care of any errors that might be encountered in the app. This default error-handling...
Read more >
A Guide to Error Handling in Express.js | Scout APM Blog
Error handling often doesn't get the attention it deserves. Mishandled errors can lead to a bad UX and negatively affect your business.
Read more >
Error Handling in Express - Reflectoring
Any error in the route handlers gets propagated through the middleware stack and is handled by the last middleware function which can be...
Read more >
Express error middleware without `next()`? - Stack Overflow
Now this middleware is reached only if I'm using next(error) (where error is object of type Error ) in my router. On the...
Read more >
Using Express.js Routes for Promise-based Error Handling
The approach presented here allows for more elegant route handlers than we started with and a single point of processing results and errors—even...
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