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


Talk Python's Mastodon Michael Kennedy's Mastodon