From the course: Build REST APIs with FastAPI

Unlock this course with a free trial

Join today to access over 24,200 courses taught by industry experts.

Sending errors

Sending errors

Most of the time, FastAPI will handle errors for you. But in some cases, you want more fine-tuned control about errors. Here I have a server that calculates the frequency of characters in the text that you send. So character frequency gets a text which is a string, and if the text is empty, it raises a frequency error, this one. Otherwise, we calculate the frequencies and we add one for every character that we add. And finally, we return it. And our handler is very simple. It gets a text as a str and then return the character frequency. So fastapidev /Ch04/04_04/server.py. Okay. And now I don't want to make this public. So if I was going to send a request with Mississippi, for example. I'm going to hit Okay, and I see that there are four Is, four Ss, two Ps, and one M in Mississippi. But if I'm going to send an empty text, I see text equal nothing here. This is going to be internal server error because this is an exception that FastAPI does not recognize. It's not the FastAPI HTTP…

Contents