Skip to content

Commit

Permalink
Adding controller utilities module
Browse files Browse the repository at this point in the history
  • Loading branch information
bergsalex committed Nov 5, 2024
1 parent 99008e2 commit e59082f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/geneweaver/api/controller/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Utilities for FastAPI Controller."""

from fastapi import HTTPException

from . import message as api_message

EXCEPTION_MAP = {
api_message.ACCESS_FORBIDDEN: HTTPException(
status_code=403, detail=api_message.ACCESS_FORBIDDEN
),
api_message.INACCESSIBLE_OR_FORBIDDEN: HTTPException(
status_code=404, detail=api_message.INACCESSIBLE_OR_FORBIDDEN
),
api_message.RECORD_NOT_FOUND_ERROR: HTTPException(
status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR
),
api_message.RECORD_EXISTS: HTTPException(
status_code=412, detail=api_message.RECORD_EXISTS
),
}


def raise_http_error(response: dict) -> None:
"""Raise HTTPException based on response message.
:param response: dict
raises: HTTPException if "error" key is in response dict.
"""
if "error" in response:
try:
raise EXCEPTION_MAP[response.get("message")]
except KeyError:
raise HTTPException(
status_code=500, detail=api_message.UNEXPECTED_ERROR
) from None

0 comments on commit e59082f

Please sign in to comment.