Modern APIs with FastAPI and Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Status codes and responses
0:00 When everything goes right, all we have to do is return some piece of data, maybe a pydantic data model or a dictionary or something like that,
0:08 And FastAPI will convert that over to the data that we're going to send back in JSON typically, and it's gonna send a 200
0:16 all ok status could. But what happens if something goes wrong? We can't just return a different style of data.
0:22 That's not enough. So what we're gonna do is actually return explicit responses from
0:28 FastAPI. So here what we're doing is we're saying we're gonna try to do this processing and return
0:33 a dictionary correctly. And if for some reason that doesn't work, we'll catch any validation errors and say,
0:39 here is the error message and the status code, which is usually 400, but sometimes it's 404. And if something else goes wrong, like
0:46 nothing we expected, but the server still crashed, we can convert that over to something like error processing,
0:51 request status code 500 and log it and then send that back. So be sure to leverage this ability to send back meaningful http Status codes.
1:01 Remember, "httpstatuses.com" tells you all about the different options, and you can go
1:06 over there and pick the right one and use this style of programming here to send that back and have a meaningful, nice,
1:13 well-behaved API. When something goes wrong, it tells you what went wrong rather than just crashing.