Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I use promises a lot in my express apps, and I was curious about how to handle errors from routes that used promises in a more general way. What I wanted to do was install some middleware that could handle any error from any route below it, whether it was a normal error thrown or an asynchronous error in the form of a rejected promise.
This is what I wanted to do:
Now, if something happens to the mongo DB, the
/thing
route will NOTres.send()
anything, simply return the promise to the error handling middleware, which will detect the error and all is (kinda) ok. At least the server returns something.Problem is that express and connect don't really think about returning the result of
next()
functions. This is, on the surface at least, fairly easy to fix. I've made a start in the files below. But the connect API is very well understood already, and while this is not a breaking change, it is a very subtle extension to the API.I've started putting
return
statements into express in the parts that matter, just to get this example working. I haven't finished yet, I intend to write tests and explore the wider scope of this change.Is appetite for this?
(and I really don't mean to start an argument about promises vs callbacks vs generators)