Handle a rejected promise and display error to user using setState()

Erik Aybar
InstructorErik Aybar
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

In this lesson we will account for errors encountered during a failed requests and display a helpful error message to the user. We will add a catch() handler (Promise.prototype.catch) to our request's promise where we will add an error message to component state and set our loading state to false. We'll add an error message to our app that will only display once an error has occurred using conditional rendering.

Instructor: [00:00] there's one new concept I want to introduce here, and that is I want to account for if we have an error, we want to make sure to display that to the user. If we have an error, we're going to pluck that out of state, we're going to display it right here for the user to see. In order to get an error to throw here, we're going to make our delete item request up here.

[00:24] We're going to say this is our fake request. That's going to fail for ID three specifically. What we're going to do here is, we will say if the ID is equal to three, the function we're going to pass the set time as is going to be reject, which will reject the promise. Otherwise, we'll resolve it as usual.

[00:45] As we can see for item one, item two, this is succeeding. If we click on item three, we have yet to handle that rejection. We're going to add for clarity here that we're beginning with null as error in our state, which is why we're not rendering anything quite yet.

[01:04] If we add a handler here on the promise through the catch, this is what we're going to do if the promise is rejected, we are going to set state error that request failed for item. Since we have the ID right here, I'll use that in the message. We can make sure this is happening as we expect.

[01:26] There we go. Now, if we click on item three, that we get our error message. Looks like we forget to clear out our loading state, let's go ahead and make sure that in any case that we set loading back to faults.

[01:42] Now, we'll go ahead, and item two will succeed, item three will fail. We're no longer loading, and we've displayed our error to the user...

egghead
egghead
~ 37 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today