Python Fundamentals

App Solution 3: Error Handling

Nina Zakharenko

Nina Zakharenko

Microsoft
Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "App Solution 3: Error Handling" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to add error handling to the code so that the user is given a useful error should the API not return the correct response.

Preview
Close

Transcript from the "App Solution 3: Error Handling" Lesson

[00:00:00]
>> Nina Zakharenko: Another important thing to do is to check the status code that we got back from our API. If it wasn't actually 200, then we're not gonna have a response.json with the items in it. We're gonna have an error in it instead. So we just wanna do a little bit of checking here.

[00:00:21]
And we're gonna say, if the status code is not equal to 200, we're just gonna raise our own exception and exit out. We're gonna talk more about exceptions tomorrow, but until then I'm just gonna say, raise a runtime error and I'm gonna pass in a message that says, an error occurred.

[00:00:47]
>> Nina Zakharenko: And oops, I want to get the status code from my response. So I want to make sure that I have that variable defined. So the status_code = response.status_code, just like we looked at when we used the dog API. Okay, we'll say, an error occurred and the status code was, and then we'll just print that out.

[00:01:15]
And this runtime error, we're not doing that try-catch sort of thing, it's just gonna exit our program. There is really nothing else to do if the github API returned an error. Otherwise, we're gonna do this code here. So technically, I don't need an else statement here. If this error occurs it's just gonna quit.

[00:01:41]
But I like being very explicit about the situation. So I'm going to put an else here, and then tab in this code.
>> Nina Zakharenko: Okay, and let's see if this works.
>> Nina Zakharenko: Looks like I did not define the variable stars right. So I'm putting in sort, I think I had stars as a string here and I just forgot to change it.

[00:02:17]
So I'm gonna update that to use the variable. Let's try it again. And it works, awesome. So we are able to use a pretty powerful API. The github search API lets you get a ton of data about publically available repos. The great thing about this API is you can use it unauthenticated.

[00:02:40]
It is really rate limited, so you can't constantly hammer it, because that would just be a denial of service attack, right? So gitub cuts off unauthenticated requests for the search API at about ten requests per minute. And for their regular API, I think it's about 50 or 60 requests per hour until they cut you off, but that was plenty for us.

[00:03:05]
I just wanna show you one more thing here in this code. So here I'm saying, status_code = reponse.status_code. But just for the time being, let's say our status_code. Let's just short circuit this and say our status code is 500, right? 500 is a server error. When we do this and we run our python file, we'll see an instant,

[00:03:41]
>> Nina Zakharenko: You'll see this, Status code was: {status_code}. What do you think that means?
>> Student: We didn't have the f.
>> Nina Zakharenko: I forgot my f string, yeah. So let me put that in there and run it one more time. Okay, we'll see, an error occurred. Status code was: 500.

[00:03:58]
We just cleanly exited our program.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now