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.

Handle rejected promises

See original GitHub issue

I must be missing something here. If I return a rejected promise from the loader of a component, nothing seems to work as expected.

On the server side, loadOnServer itself is not rejected but rather it continues to resolve the promise and so the component tree gets rendered without the required data. I then get other exceptions due to the missing data which does reject the entire promise. (as expected).

loadOnServer({ ...renderProps, store }).then(() => {
  // Even when the loader promise is rejected this code runs..
})
.catch(err => {
 // this would run eventually when something breaks in a component due to the missing data..
})

On the client side, I am not even sure what to expect - clearly there is no extension point where I can change the routing and redirect to an error page. How should that be handled on the client?

<ReactReduxProvider store={store}>
  <Router  {...renderProps}
    render={(props) => <ReduxAsyncConnect {...props}
                        render={applyRouterMiddleware(useScroll(cusotmScrollBehaviour))}
                   />}
    routes={getRoutes(context)}/>
</ReactReduxProvider>

Any insights on this would be great (I was digging in the sources for a while, but I don’t think there is any special handling for this situation) Thanks!!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tswaterscommented, Mar 13, 2017

If someone is looking for a catch behaviour, you can pull out the error from state and throw if it exists… something like the following should work. This tosses the first error encountered, but could easily be reworked into aggregating all the errors into an array.

loadOnServer({ ...renderProps, store }).then(() => {
  const {loadState} = store.getState().reduxAsyncConnect
  const err = Object.keys(loadState).reduce((memo, key) => memo || loadState[key].error, null)
  if (err) { throw error }
  // whatever else
})
.catch(err => {
  // a wild error handler appeared!
  next(err)
})

I’d much prefer if it automagically went into the catch block myself, but can appreciate the reasoning / breaking changes / potential for multiple errors, etc. for not doing it like that.

1reaction
vpanjganjcommented, Jan 30, 2017

@yoadsn ,I think if a component (UI) has to visually respond to a data load failure, it is enough reason to include that in the state and implement the behaviour in the component. To address your concern about redundant code to handle data errors; you can use Higher-ordered components to reuse the logic anywhere necessary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling with promises - The Modern JavaScript Tutorial
Promise chains are great at error handling. When a promise rejects, the control jumps to the closest rejection handler.
Read more >
How to handle promise rejections - Flavio Copes
How to handle promise rejections · fetch('/data.json') then · thePromise catch error · thePromise .then(response => { console. · const thePromise ...
Read more >
Promise.reject() - JavaScript - MDN Web Docs
The static Promise.reject function returns a Promise that is rejected. For debugging purposes and selective error catching, it is useful to ...
Read more >
Tracking Unhandled Promise Rejections - TrackJS
All kinds of things can go wrong in the real world, and the network is never reliable. If an error condition arises inside...
Read more >
Handling Js promise rejection - javascript - Stack Overflow
Handling Js promise rejection · 1. Use try/catch. – Trash Can. Apr 14, 2017 at 7:19 · 1. You'll have to throw the...
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