2 changed files with 28 additions and 8 deletions
@ -1,23 +1,42 @@ |
|||
from fastapi import FastAPI, HTTPException |
|||
from fastapi import FastAPI, HTTPException, Request |
|||
from fastapi.exceptions import RequestValidationError |
|||
from fastapi.responses import PlainTextResponse |
|||
from fastapi.responses import JSONResponse |
|||
from starlette.exceptions import HTTPException as StarletteHTTPException |
|||
from fastapi.encoders import jsonable_encoder |
|||
from starlette import status |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.exception_handler(StarletteHTTPException) |
|||
async def http_exception_handler(request, exc): |
|||
return PlainTextResponse(str(exc.detail), status_code=exc.status_code) |
|||
|
|||
async def http_exception_handler(request: Request, exc: StarletteHTTPException): |
|||
""" |
|||
Handles all HTTPException (Starlette) errors. |
|||
Returns a JSON response with status code and detail. |
|||
""" |
|||
return JSONResponse( |
|||
status_code=exc.status_code, |
|||
content={"message": exc.detail} |
|||
) |
|||
|
|||
@app.exception_handler(RequestValidationError) |
|||
async def validation_exception_handler(request, exc): |
|||
return PlainTextResponse(str(exc), status_code=400) |
|||
async def validation_exception_handler(request: Request, exc: RequestValidationError): |
|||
""" |
|||
Handles validation errors for request data. |
|||
Returns a JSON response with 400 status and error details. |
|||
""" |
|||
return JSONResponse( |
|||
status_code=status.HTTP_400_BAD_REQUEST, |
|||
content=jsonable_encoder({"errors": exc.errors(), "body": exc.body}) |
|||
) |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
async def read_item(item_id: int): |
|||
|
|||
if item_id == 3: |
|||
raise HTTPException(status_code=418, detail="Nope! I don't like 3.") |
|||
raise HTTPException( |
|||
status_code=418, |
|||
detail="Nope! I don't like 3." |
|||
) |
|||
return {"item_id": item_id} |
|||
|
|||
Loading…
Reference in new issue