From the course: Introduction to Web APIs

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

How to handle errors

How to handle errors - REST Tutorial

From the course: Introduction to Web APIs

How to handle errors

- [Instructor] Let's look at how we can handle possible errors when we're requesting data from an API. We're going to use the status codes we just talked about to help us deal with errors. We know that if everything goes well with the resource we're requesting, then we'll get an okay status code of 200. So, let's check for that. We'll use an if else statement to check to see if the status code is 200. I'm going to write this in my first then method. So, if our response has a status, that status equal to 200 double equal sign inside the curly brackets, then we want to return our responses json. So, I'm just going to cut it here and paste it inside. Else if we get some other status response, then we want to print out some error message to the console. Console.log Whoops problem. So, if our status code is 200, then we'll return the responses as json and then proceed with our next then function. If not, then we know…

Contents