Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Using our database with the SQLAlchemy ORM
Lecture: Error handling in the API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's do one more thing to make our order API just a little bit
0:03 more polished. There's all kinds of things that could go wrong here.
0:07 And they would appear to the end user to the consumer,
0:10 the API as a server side crash.
0:12 But they wouldn't always be, for example,
0:14 if we pass in the wrong information here.
0:17 Like we forget to put some data into our JSON pydantic is going to crash and
0:22 say that was wrong through a validation error,
0:24 which is going to get converted to just a Server 500.
0:27 That's not what we want. Let's go over here and put this into a try
0:30 except block and we'll say, except validationError from pydantic as ve and
0:43 take a pass for a second.
0:45 Maybe we want to particularly handle crashes from the database layer,
0:49 so we might want to do something there that we can't do a lot about.
0:51 But we can do that as some kind of thing there as a SQLAlchemy error
0:59 Then we can do a catch all like,
1:04 Well, we tried the others who knows what happened here and what I'm gonna do
1:08 is, I'm gonna define two pieces of information.
1:11 The error body, which is going to be a string with the error code,
1:16 which is a status code. So in this case,
1:19 the validation error happened, and 422 is invalid data,
1:23 Incorrect data type of code.
1:24 We can go with that. And for this,
1:26 we would actually give it some kind of JSON that it can work with.
1:31 So we're gonna go over here or say that there is an an error and it's
1:35 going to be the string of just it's not always a good idea.
1:38 I'm just going to pass back the string representation of the error message straight back to
1:43 them, and I think it's going to be okay.
1:45 One challenge, though, is this is going to have a bunch of new lines
1:48 and it potentially might come back looking a bit were someone to replace the new lines
1:51 with just a space or something like that.
1:57 And we can do that similarly over here,
1:59 this one as see, this is the database crashed.
2:04 Maybe they didn't pass in something that was like a constraint that wasn't met.
2:07 But I'm just going to say this is a 500 and again down here,
2:11 we might do something different and say the regular exception versus SQLAlchemy error.
2:17 But that's when we got and then remember up here,
2:20 we're turning already in the good case.
2:21 So down here, whatever happened in the errors were going to just return some kind
2:25 of error. So flat, start response or use JSON over here,
2:33 they dump s, turn this into a string error body.
2:40 The status is going to be error code,
2:43 and the mimetype is going to be application/json.
2:48 So you can read that little easier.
2:49 And these huge fonts, will wrap it around like that.
2:53 I guess we could test one more time just to see that something is coming back
2:56 with postman. Let's go over to our body,
3:01 for example, and remove the name.
3:03 Remember, the name is required to send that over.
3:06 What kind of response did we get?
3:07 422 unprocessed double entity.
3:11 It was well formed, but there was some issues,
3:13 right? The validation error, cake order,
3:17 customer name field is required, which is missing.
3:20 So we got our error code and our error message.
3:22 Let's put that back extra things are good,
3:26 they are, perfect. I feel like having that little touch of proper error handling instead
3:31 of just all the time saying the server crashed.
3:33 If even it's their fault, Yeah,
3:35 there's a little bit nicer, I think.