
FastAPI
Server error codes (500-599) indicate that the server encountered a problem and couldn't fulfill the request.
| Code | Name | Meaning |
|---|---|---|
| 500 | Internal Server Error | Generic server error |
| 501 | Not Implemented | Feature not implemented |
| 502 | Bad Gateway | Gateway/proxy error |
| 503 | Service Unavailable | Server temporarily unavailable |
| 504 | Gateway Timeout | Gateway timed out |
@app.get("/items")
async def list_items():
try:
items = db.list_items()
return items
except DatabaseError:
# Database is down
raise HTTPException(
status_code=500,
detail="Database connection failed"
)Use 5xx codes when:
# ❌ Don't expose internal details
raise HTTPException(
status_code=500,
detail=f"Database error: {str(e)}"
)
# ✅ Keep errors generic
raise HTTPException(
status_code=500,
detail="Internal server error"
)Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward