💭 Handling Errors - FastAPI =========================== !https://fastapi.tiangolo.com/tutorial/handling-errors/ Date: December 17, 2023 Handling Errors - FastAPI FastAPI framework, high performance, easy to learn, fast to code, ready for production fastapi.tiangolo.com [1] This page shows how to customize your fastapi errors. I found this very useful to setup common templates so that I can return the same 404's both programatically and by default, so it all looks the same to the end user. ``` python from fastapi import FastAPI, Request from fastapi.responses import JSONResponse class UnicornException(Exception): def __init__(self, name: str): self.name = name app = FastAPI() @app.exception_handler(UnicornException) async def unicorn_exception_handler(request: Request, exc: UnicornException): return JSONResponse( status_code=418, content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."}, ) @app.get("/unicorns/{name}") async def read_unicorn(name: str): if name == "yolo": raise UnicornException(name=name) return {"unicorn_name": name} ``` !!! note This post is a thought [2]. It's a short note that I make about someone else's content online #thoughts [3] References: [1]: https://fastapi.tiangolo.com/tutorial/handling-errors/ [2]: /thoughts/ [3]: /tags/thoughts/