一键导入
fastapi-error-handling
Error handling patterns for BuscaMedicos FastAPI. HTTPException, custom exceptions, and consistent error response format.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Error handling patterns for BuscaMedicos FastAPI. HTTPException, custom exceptions, and consistent error response format.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Authentication patterns for BuscaMedicos web frontend. JWT handling, login, register, session persistence, and role-based redirection.
Nuxt 4 + Vue 3 + Vuetify frontend for BuscaMedicos. Pages, layouts, components, composables, and stores patterns.
Git add, commit and push workflow for BuscaMedicos. Streamlined commands with CI env vars for non-interactive Git operations.
Alembic migration workflow for BuscaMedicos. Create, autogenerate, and run migrations following the step2-step8 architecture.
Testing patterns for BuscaMedicos. Pytest with async support, fixtures, and project-specific test conventions.
Docker Compose development workflow for BuscaMedicos. Start/stop/verify containers, check logs, rebuild with --build flag.
| name | fastapi-error-handling |
| description | Error handling patterns for BuscaMedicos FastAPI. HTTPException, custom exceptions, and consistent error response format. |
| license | MIT |
Error handling patterns for BuscaMedicos. Consistent error responses, custom exception classes, and proper HTTP status codes.
Use this skill when:
from fastapi import HTTPException, status
@router.get("/{resource_id}")
async def get_resource(resource_id: str, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Model).where(Model.id == resource_id))
obj = result.scalar_one_or_none()
if not obj:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Resource not found"
)
return obj
| Code | Use Case |
|---|---|
| 400 | Bad request / validation error |
| 401 | Unauthorized (no/invalid auth) |
| 403 | Forbidden (insufficient permissions) |
| 404 | Resource not found |
| 409 | Conflict (duplicate resource) |
| 422 | Validation error (Pydantic) |
| 500 | Internal server error |
class NotFoundError(Exception):
def __init__(self, resource: str, resource_id: str):
self.resource = resource
self.resource_id = resource_id
class DuplicateResourceError(Exception):
def __init__(self, resource: str, identifier: str):
self.resource = resource
self.identifier = identifier
from fastapi.responses import JSONResponse
@router.get("/error-example")
async def error_example():
return JSONResponse(
status_code=400,
content={"detail": "Error message", "code": "ERROR_CODE"}
)
Pydantic automatically returns 422 for validation failures with this format:
{
"detail": [
{
"loc": ["body", "field_name"],
"msg": "field required",
"type": "value_error.missing"
}
]
}
Add to main.py:
from fastapi import Request
from fastapi.responses import JSONResponse
@app.exception_handler(NotFoundError)
async def not_found_handler(request: Request, exc: NotFoundError):
return JSONResponse(
status_code=404,
content={
"detail": f"{exc.resource} {exc.resource_id} not found",
"code": "NOT_FOUND"
}
)
except Exception: pass - always log or handle properlydetail field