Trung BìnhFastAPI iconFastAPI

FastAPI xử lý lỗi validation và business errors như thế nào?

Lỗi validation request tự động trả 422 với chi tiết field lỗi. Business error nên dùng HTTPException hoặc custom exception + exception handler để response nhất quán.

Ví dụ:

python
if not user:
    raise HTTPException(status_code=404, detail="User not found")

Không nên trả { "error": ... } thủ công với status 200.

API client cần status code đúng để retry, log, alert và xử lý UX chính xác.

Xem toàn bộ FastAPI cùng filter theo level & chủ đề con.

Mở danh sách FastAPI