From 01e994aba2a02a2c475ae0c413021da6b5ee64f3 Mon Sep 17 00:00:00 2001 From: Juan Carlos Jose Camacho Date: Mon, 4 Mar 2024 16:04:22 -0600 Subject: [PATCH] [DH-5519] Remove from the error response dict and create a log --- dataherald/utils/error_codes.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/dataherald/utils/error_codes.py b/dataherald/utils/error_codes.py index 1752b064..f4c4cb7e 100644 --- a/dataherald/utils/error_codes.py +++ b/dataherald/utils/error_codes.py @@ -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", @@ -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, }, )