-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |