-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample_exception.py
More file actions
50 lines (35 loc) · 1.34 KB
/
example_exception.py
File metadata and controls
50 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, model_validator
from typing import Optional
app = FastAPI()
class ErrorResponse(BaseModel):
message: str
class Item(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
price: float = Field(..., gt=0) # Ціна повинна бути більшою за 0
tax: Optional[float] = None
@model_validator(mode="before")
def validate_item(cls, values):
name = values.get("name")
price = values.get("price")
if not name:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Name is required."
)
return values
@app.post("/items/", responses={400: {"model": ErrorResponse}})
async def create_item(item: Item):
# Якщо всі перевірки пройшли успішно, повертаємо елемент
return item
# Обробник для HTTPException
@app.exception_handler(HTTPException)
def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)