Modern APIs with FastAPI and Python Transcripts
Chapter: Error handling and performance
Lecture: Error responses

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