Skip to content

Commit

Permalink
[DH-5519] Remove from the error response dict and create a log (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjc712 authored Mar 4, 2024
1 parent 5e7a5de commit 09f65c6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions dataherald/utils/error_codes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging

from fastapi.responses import JSONResponse

logger = logging.getLogger(__name__)

ERROR_MAPPING = {
"InvalidId": "invalid_object_id",
"InvalidDBConnectionError": "invalid_database_connection",
Expand All @@ -25,16 +29,20 @@ def __init__(self, message, description=None):


def error_response(error, detail: dict, default_error_code=""):
error_code = ERROR_MAPPING.get(error.__class__.__name__, default_error_code)
description = getattr(error, "description", None)
logger.error(
f"Error code: {error_code}, message: {error}, description: {description}, detail: {detail}"
)

detail.pop("metadata", None)

return JSONResponse(
status_code=400,
content={
"error_code": ERROR_MAPPING.get(
error.__class__.__name__, default_error_code
),
"error_code": error_code,
"message": str(error),
"description": error.description
if isinstance(error, CustomError)
else None,
"description": description,
"detail": detail,
},
)

0 comments on commit 09f65c6

Please sign in to comment.