Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catches errors from Promises / Async functions #47

Closed
wants to merge 1 commit into from

Conversation

leebyron
Copy link
Contributor

@leebyron leebyron commented Aug 14, 2016

When returning Promises from a route handler either explicitly or implicitly by using Async Functions, default error handling is lost.

// Error caught and forwarded as expected
app.get('/', (req, res) => {
  throw new Error('Oh crap!')
})

// Error swallowed and lost
app.get('/', async (req, res) => {
  throw new Error('Oh crap!')
})

This PR began as expressjs/express#3053, however it was a breaking change and Route/Layer have been moved to router for express@5.

@blakeembrey
Copy link
Member

blakeembrey commented Aug 14, 2016

Duplicate of #32 and probably a couple of other PRs. If you're going to build this feature, just remember that promises can be rejected with a falsy value and this doesn't handle it right now.

When returning Promises from a route handler either explicitly or implicitly by using Async Functions,default error handling is lost.

```
// Error caught and forwarded as expected
app.get('/', (req, res) => {
  throw new Error('Oh crap!')
})

// Error swallowed and lost
app.get('/', async (req, res) => {
  throw new Error('Oh crap!')
})
```
@leebyron
Copy link
Contributor Author

Updated to handle falsey rejection as well.

I really appreciate the work you put into #32, but my hope is that this is a minimal fix for this specific issue to incrementally introduce working with Promises into router/express while preserving as much existing behavior as possible to reduce migration pain.

As @dougwilson pointed out in expressjs/express#3053, with this PR the only major detail library authors will need to consider is that if they wrap middleware or route handlers that they return the result of calling the original middleware/handler.

@leebyron
Copy link
Contributor Author

leebyron commented Aug 14, 2016

remember that promises can be rejected with a falsy value and this doesn't handle it right now.

This is a great point, and I realized a similar issue currently exists for throwing falsey values which is something that's possible but pretty uncommon. Let me know if you would like me to open an issue for that or approach a fix.

@leebyron
Copy link
Contributor Author

Let me know if there are other concerns - I'm happy to add more tests or add other mitigations.

@leebyron
Copy link
Contributor Author

Just revisiting this since it's been a month or so - any evolution of thinking on this? Other concerns to address?

@dougwilson dougwilson added the pr label Nov 3, 2016
@olalonde
Copy link

Bumppp...

@gajus
Copy link

gajus commented Sep 25, 2017

@dougwilson Is any help needed here?

@jmar777
Copy link

jmar777 commented Sep 25, 2017

I'm not sure if it's helpful or not, but I've been using an outside implementation of this in production for awhile now: https://github.com/jmar777/node-async-router. If nothing else, it might be useful to @olalonde, if you were looking for a similar solution.

It handles the error scenario mentioned by the OP (relevant test case).

@ajsharp
Copy link

ajsharp commented Apr 24, 2018

What's the status of this? Are there any plans to pull this in? @blakeembrey

@wesleytodd
Copy link
Member

LGTM :)

@dougwilson
Copy link
Contributor

This PR is going to land in the new 2.0 branch and release as an alpha version for preview this upcoming week.

@dougwilson dougwilson self-assigned this Jul 19, 2018
@dougwilson dougwilson added this to the 2.0 milestone Jul 19, 2018
@dougwilson
Copy link
Contributor

Sorry for the delay. I just rebased the 2.0 branch and now getting this rebased and up there. It looks like I don't have permission to push to the fork for this PR, so it won't show merged after I merge it, just "closed", but it'll be merged into 2.0.

@dougwilson
Copy link
Contributor

I have written up some docs for this PR now (it had no docs). Now just adding more tests because the tests are just on a tiny part of the API surface, but promises are being handled with this PR on a large part of the API so needs the tests added to all those areas.

@dougwilson
Copy link
Contributor

Alright, I got this committed to the 2.0 branch now. Just checking a few more things, but otherwise will close this PR as merged at release the first 2.0 alpha 🎉

@dougwilson
Copy link
Contributor

Published to npm as version 2.0.0-alpha.1

@dougwilson dougwilson closed this Jul 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants