Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions app/routers/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ async def validation_error_handler(request: Request, exc: RequestValidationError
# FASTAPI HTTP EXCEPTIONS
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
err_msg = ""
if hasattr(exc, "detail") and exc.detail:
err_msg = exc.detail

if exc.status_code == 304:
return JSONResponse(status_code=304, content=None, headers=exc.headers or {})
Expand All @@ -98,7 +101,7 @@ async def http_exception_handler(request: Request, exc: HTTPException):
request=request,
status=401,
title="Unauthorized",
detail="Bearer token is missing or invalid.",
detail=err_msg or "Bearer token is missing or invalid.",
problem_type="unauthorized",
extra_headers={"WWW-Authenticate": "Bearer"},
)
Expand All @@ -108,7 +111,7 @@ async def http_exception_handler(request: Request, exc: HTTPException):
request=request,
status=403,
title="Forbidden",
detail="Caller is authenticated but lacks required role.",
detail=err_msg or "Caller is authenticated but lacks required role.",
problem_type="forbidden",
)

Expand All @@ -117,7 +120,7 @@ async def http_exception_handler(request: Request, exc: HTTPException):
request=request,
status=404,
title="Not Found",
detail=exc.detail or "Invalid resource identifier.",
detail=err_msg or "Invalid resource identifier.",
problem_type="not-found",
)

Expand All @@ -126,7 +129,7 @@ async def http_exception_handler(request: Request, exc: HTTPException):
request=request,
status=405,
title="Method Not Allowed",
detail="HTTP method is not allowed for this resource.",
detail=err_msg or "HTTP method is not allowed for this resource.",
problem_type="method-not-allowed",
extra_headers={"Allow": "GET, HEAD"},
)
Expand All @@ -136,29 +139,31 @@ async def http_exception_handler(request: Request, exc: HTTPException):
request=request,
status=409,
title="Conflict",
detail=exc.detail or "Conflict occurred.",
detail=err_msg or "Conflict occurred.",
problem_type="conflict",
)

# Generic fallback
return problem_response(
request=request,
status=exc.status_code,
title=exc.detail or "Error",
detail=exc.detail or "An error occurred.",
title=err_msg or "Error",
detail=err_msg or "An error occurred.",
problem_type="generic-error",
)

# STARLETTE HTTP EXCEPTIONS
@app.exception_handler(StarletteHTTPException)
async def starlette_handler(request: Request, exc: StarletteHTTPException):

err_msg = ""
if hasattr(exc, "detail") and exc.detail:
err_msg = exc.detail
if exc.status_code == 404:
return problem_response(
request=request,
status=404,
title="Not Found",
detail="Invalid resource identifier.",
detail=err_msg or "Invalid resource identifier.",
problem_type="not-found",
)

Expand All @@ -167,16 +172,16 @@ async def starlette_handler(request: Request, exc: StarletteHTTPException):
request=request,
status=405,
title="Method Not Allowed",
detail="HTTP method is not allowed for this resource.",
detail=err_msg or "HTTP method is not allowed for this resource.",
problem_type="method-not-allowed",
extra_headers={"Allow": "GET, HEAD"},
)

return problem_response(
request=request,
status=exc.status_code,
title=exc.detail or "Error",
detail=exc.detail or "An error occurred.",
title=err_msg or "Error",
detail=err_msg or "An error occurred.",
problem_type="generic-error",
)

Expand Down