-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror-handling-logging.py
33 lines (29 loc) · 1.05 KB
/
error-handling-logging.py
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
import logging
from fastapi import HTTPException
from fastapi.responses import JSONResponse
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Custom exception handler
async def http_exception_handler(request, exc):
logger.error(f"HTTP error occurred: {exc.detail}")
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
# Example usage in a route
@app.get("/example")
async def example_route():
try:
# Your code here
result = some_function()
logger.info(f"Successfully processed request: {result}")
return {"result": result}
except SomeSpecificError as e:
logger.error(f"Specific error occurred: {str(e)}")
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.exception("Unexpected error occurred")
raise HTTPException(status_code=500, detail="Internal server error")
# Add this to your main.py
app.add_exception_handler(HTTPException, http_exception_handler)