Стандартная структура: {"error": {"code": "NOT_FOUND", "message": "User not found", "details": {...}}}. В FastAPI: raise HTTPException(status_code=404, detail="Not found"). Кастомные exceptions: class UserNotFoundError(Exception). Exception handler: @app.exception_handler(UserNotFoundError) def handler(request, exc): return JSONResponse(status_code=404, content={"error": str(exc)}). Глобальный handler для 500: логируй traceback, возвращай generic message. Validation errors: 422 с деталями полей. Не раскрывай внутренние ошибки клиенту. Sentry — для мониторинга ошибок в production.
Как обрабатывать ошибки в API?
Middle
279 просмотровAFK Offer AI
Как создать свой context manager?