Modern APIs with FastAPI and Python Transcripts
Chapter: Error handling and performance
Lecture: Error responses
0:00 Well, our weather API works. We can come over here and click this. Get a nice result here, nice response.
0:07 However, what happens if there's the smallest of mistake? Like we misspell Portland to Portlnad.
0:15 That doesn't seem right. So we probably shouldn't be getting a server crash 500 server
0:20 error message when really what happened was there's just no city named Portlnad.
0:25 So let's go fix that. So what's happening is we're getting some response back, and it's just raising a standard exception.
0:32 So let's go and add a little bit more information here. We'll go over here and add a validation. We'll have a class,
0:42 which is a validation error, that derives from exception. And let's give it actually a couple of things.
0:49 Let's go over here and say that it's going to take an error message, string, and it's gonna take a status code, which is an integer. Let PyCharm
1:01 add that to the class for us. This way, when we get one of these errors, we'll have a meaningful way to respond and let's go over here like this,
1:09 Say "super dot init" and pass the error message along as well, perfect. So now we have one of these validation errors.
1:16 We can raise these and catch these and so on. So, for example, instead of this, we can say if response dot, say that this
1:24 is a response, like so. You can say that the status code is not equal to 200. Well, something went wrong over there.
1:33 So let's raise a validation error, like that, and we'll set the error message to be like this and the status code to the status
1:44 code. Let's pick an error message. Actually, let's just let the service communicate that response dot text, like that.
1:51 Let's see if this gives us some kind of better result for our Portlnad. We come over here and we try again.
1:57 Well, it crashed again because we're not converting that to a response. We'll do that now. So over here,
2:03 let's instead of just calling this, and we can just do a return, I suppose, like that. But instead of doing this,
2:10 let's say try to do it, and then we'll have an except validation error, like so. As
2:18 ve. And then what we want to do is return some kind of response. So return fastapi dot response. Set the content to be ve dot error message, and
2:27 the status code to be ve dot error. And I believe that's just gonna be, whoops status code. Set that to just the standard,
2:36 you know, plain text response. We could put that into a dictionary, right? This could be a little bit nicer. Maybe we'll do that in the end.
2:43 But let's just see what's happening now. Try again. 404 not found. And it says 404 there but that's not the super important thing.
2:50 Let's look at what the network says When we make this call. There you go, 404 is the actual response whoever called our API got,
2:59 which is perfect. So, we got our 400. Excellent, excellent, Excellent. There's a whole bunch of other validation that we need
3:05 to add that turns out to be a lot of work. We wanna make sure that we're always passing the lower case state,
3:11 maybe, in case that matters or not have spaces or potentially commas. We use two letter abbreviations instead of just random text,
3:20 and so on. Now that turns out to be super error prone. So Let's go and just do this real quick. We're gonna throw this in and import a type here.
3:28 So we're gonna have this thing called "validate units", which is going to take a city, state, country and unit, and it's going to return
3:35 modified city, state, country and units. Yes, second one maybe needs to be optional here. So what we want to do is go and strip out the city,
3:45 make it lower case. If there's no country, make it the US, otherwise lower case it, strip it, make sure it has length two,
3:52 if not create an error message that says it has to be length two, and that's bad data you passed because it's not length two.
3:59 So we're gonna raise a validation error just like before, but now it's gonna catch that. If there's a state,
4:04 do the same thing here, the units, make sure it's lowercase. There has to be some valid units. It has to be one of these three.
4:11 If it's not, say it has to be one. All that is a lot of work. I'll copy this to save a little typing. We'll go up to the top here,
4:20 and I'll just say that equals validate all of those things. Okay, let's see if we could get this to work. So we got our Portlnad.
4:27 That's a server error on the other side. This is valid. But what if we come over and say, country equals USA?
4:35 Nope. It must be two letters, US. And look, your 400, bad data. That's fine. So we got this back good again. And what if we say "units equals metricy"?
4:46 No, no, Can't be metricy, it has to be standard, imperial or metric, right? So now we've got our validation happening again and notice
4:53 the status code, not just the error message is being passed along. Cool, Right? So now, instead of saying, "Oh, the server crashed",
4:59 if we click up here and we got Portlnad, instead what we're getting back is error messages in terms of http status codes and meaningful messages.