quick fastapi http error endpoint

#770
Raw
Author
Anonymous
Created
May 27, 2023, 11:44 p.m.
Expires
Never
Size
499 bytes
Hits
58
Syntax
Python
Private
No
from fastapi import FastAPI, HTTPException
from http import HTTPStatus


app = FastAPI()


@app.get("/status")
async def status(code: int = 200):
    """
    /status endpoint

    simulates an http error response

    /status?code=404 will return a http 404 Not Found error

    /status will return a http 200 OK

    """


    if code >= 400:
        raise HTTPException(status_code=code, detail="an error happened")

    return {"message": "everything is alright"}