This Is How You Do PROPER Exception Handling With FastAPI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever used a website or an API and you encountered a very cryptic error something went wrong or internal server error well that probably happened because the website or API didn't properly Implement custom error handling today I'm going to show you how to set up custom exception handling in fast API what I'm going to use to show this is skyall It's a simple weather API that can get the weather for any city in the world as long as it's in the data and currently the database is not very large before we start if you want to learn how to design an API like this from scratch take a look at my free design guide this teaches you the seven steps that I take whenever I design a new piece of software go to iron. codesign guide enter your email address and you get it via email python already has a whole hierarchy of exceptions that you can use but sometimes it makes more sense to use a custom hierarchy of exceptions that still needs to be part of Python's Main exception hierarchy so when you define an exception class in this case I have a sky pulse API Arrow which is the base exception that we're going to use for all possible errors that our API can raise then we also need to inherit from the python exception class that way it fits well within Python's exception framework in this case the only thing I did in the skyall API error is that there is a message and the name that's being stored into variables that's part of the exception object and there we already have our custom exception but of course now that you have this you can take this a step further by filling out the hierarchy because this is kind of generic and still very cryptic so I still have a surface error which is the general error but then I also have more specific types of Errors like entity does not exist error or entity already exist error invalid operation error authentication failed error you can add many more of these different arot types depending on what your application does and this case I haven't really provided any additional details but you can also pass custom data to each of these different exceptions for example the invalid operation error could have a custom instance variable that contains the operation that you try to run so that we know where the problem is the reason to have this Sky pulse API error at the top is that not only have we now defined a custom hierarchy for our API we also provide a way of catching all errors related to The skyall Domain so if we have have some code that interacts with the sky pulse API code we could catch any Sky pulse API error so that we're sure that if there is an issue we are catching it without having to catch any exception also we might in the future add a second hierarchy with other types of errors and then we can handle those things separately in fast API when an exception is raised by default fast API default error Handler takes care of it but you can replace that by a custom error Handler as well this is the main file of my API where I also do things like adding the routers Etc so part of the main file is adding the routers like you see here but another thing that I've done here is I've created a function create exception Handler that gets a status code and an initial detail and that handles particular types of exceptions so in this case we expect a sky pulse API error and it's going to turn that into a Json respond by looking at things like the message and then what you can do in fast API is ADD exception handlers for specific classes so for each of these specific types of Errors I add an exception Handler like entity doesn't exist in val operation etc etc in the exception handling code I return a Json response and I also use a logger now of course you have to be careful here that you're not returning any sensitive data so you might want to filter out things like passwords and email addresses so now let's take a look at how this works so here I have one of the routers this is the router to get weather information so there's the standard crowd operations so let's see what happens if we raise an error so this get request I'm going to raise a service error that the service is unavailable and that's the message and I can also give this a name like so let me save this and now let me go to the documentation so this is one of the nice things about fast API that we get automatically a Swagger UI which is really nice so let me go to the particular get request that's API V1 weather and then let's try that so now I'm going to try this and then execute and now we're going to see that we get an error internal service error and there we have the detail service is unavailable so that's the power of custom exceptions and the nice thing about the exception Handler that I created is that it also logs the error so if I go to my Docker image that's currently running the surface you can see that I get the error right here so I can also see it back in the logs so overall if you spend a bit of time setting up your custom exceptions it's going to be a way better experience for the users of your API and it's not that hard to do as you've seen in this example now I've put all the example code in the examples repost story so you can take a look and copy paste whatever you need finally when you use these types of custom exceptions make sure you remember that a you create specific exceptions and not to generic ones specific is better because they give more precise information you can add data to your exception so you can do something with the data in your exception handling code and finally make sure you raise exceptions as early as possible because if there is some sort of error in your system you want to catch that error as early on as possible and provide that information back to the user and not let that error propagate through your system resulting in maybe even worse situation that's also called applying the fail fast principle if you want to learn more about how to properly apply fail fast check out this video next thanks for watching and see you soon
Info
Channel: ArjanCodes
Views: 15,853
Rating: undefined out of 5
Keywords: fastapi, custom exceptions in python, custom exceptions, fastapi custom exceptions, custom exceptions fastapi, custom exceptions python, fastapi tutorial, fastapi tutorial python, fastapi python, exception handling, python exceptions, python tutorial, python exception handling, python error, python error handling, python requests, learn python, api python, fastapi python tutorial, exception handling in python, python exceptions explained, python exceptions tutorial
Id: 7MHDDOrDx-w
Channel Id: undefined
Length: 6min 9sec (369 seconds)
Published: Tue Apr 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.