gabrielbull/react-router-server

unhandled promise rejection... can't be caught

tswaters opened this issue · 2 comments

I encountered this serendipitously while using react-helmet with a <FormattedMessage/> from react-intl.... react-helmet doesn't like this at all, and will throw back an error if it doesn't get a simple string.

Anyway, using react-router-server there is an unhandled promise rejection logged to the console. I attempted to add a catch block to the renderToString call, but this has no effect.

It appears there needs to be a try{}catch{} wrapping renderToString here -- https://github.com/gabrielbull/react-router-server/blob/master/src/renderer/renderPass.js#L36-L40 -- and if an error occurs, call into context.reject with the error.

This might also crop up with things like invariant errors or who knows what else. When it happens, the browser is left hanging with a pending request.

Actually, I think this is something wrong with my own project.... I forked in an attempt to add a failing test that highlights the problem, but it works there. I'll re-open if I see a problem with react-router-server

Ah, it was my test that was wrong - this is indeed an issue.

It can be easily recreated by adding a throw new Error() inside one of the render functions in react-router-server-complex-example... and unhandled promise rejection occurs and the process is taken down.

PR incoming.