From 5ed5c38d3d00fb2ea0a71c401bfa4781b45ce236 Mon Sep 17 00:00:00 2001 From: Pradipta Ghoshal <50803990+ProCode2@users.noreply.github.com> Date: Sat, 10 Feb 2024 15:04:09 +0530 Subject: [PATCH] ABDM: Log the errors instead of sending them as a response (#1821) * chore: log ABDM errors and send error message to client * chore: pre-commit * refactor: remove erro key as not needed, fix typo * undo changes in data/mediabase.json * refactor: return error messages properly --------- Co-authored-by: Aakash Singh --- care/abdm/api/viewsets/auth.py | 17 ++++++++++++-- care/abdm/api/viewsets/healthid.py | 36 ++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/care/abdm/api/viewsets/auth.py b/care/abdm/api/viewsets/auth.py index 5625824126..5802a493db 100644 --- a/care/abdm/api/viewsets/auth.py +++ b/care/abdm/api/viewsets/auth.py @@ -1,4 +1,5 @@ import json +import logging from datetime import datetime, timedelta from django.core.cache import cache @@ -14,6 +15,8 @@ from care.facility.models.patient_consultation import PatientConsultation from config.authentication import ABDMAuthentication +logger = logging.getLogger(__name__) + class OnFetchView(GenericAPIView): permission_classes = (IsAuthenticated,) @@ -25,7 +28,14 @@ def post(self, request, *args, **kwargs): try: AbdmGateway().init(data["resp"]["requestId"]) except Exception as e: - return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST) + logger.warning( + f"Error: OnFetchView::post failed while initialising ABDM Gateway, Reason: {e}", + exc_info=True, + ) + return Response( + {"detail": "Error: Initialising ABDM Gateway failed."}, + status=status.HTTP_400_BAD_REQUEST, + ) return Response({}, status=status.HTTP_202_ACCEPTED) @@ -346,10 +356,13 @@ def post(self, request, *args, **kwargs): } ) except Exception as e: + logger.warning( + f"Error: RequestDataView::post failed to notify (health-information/notify). Reason: {e}", + exc_info=True, + ) return Response( { "detail": "Failed to notify (health-information/notify)", - "error": str(e), }, status=status.HTTP_400_BAD_REQUEST, ) diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index 2957f93914..318fa7e3b7 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -1,5 +1,6 @@ # ABDM HealthID APIs +import logging from datetime import datetime from drf_spectacular.utils import extend_schema @@ -31,6 +32,8 @@ from config.auth_views import CaptchaRequiredException from config.ratelimit import ratelimit +logger = logging.getLogger(__name__) + # API for Generating OTP for HealthID class ABDMHealthIDViewSet(GenericViewSet, CreateModelMixin): @@ -363,8 +366,14 @@ def link_via_qr(self, request): } ) except Exception as e: + logger.warning( + f"Error: ABDMHealthIDViewSet::link_via_qr failed to fetch modes. Reason: {e}", + exc_info=True, + ) return Response( - {"detail": "Failed to fetch modes", "error": str(e)}, + { + "detail": "Failed to fetch modes", + }, status=status.HTTP_400_BAD_REQUEST, ) @@ -419,8 +428,15 @@ def get_new_linking_token(self, request): } ) except Exception as e: + logger.warning( + f"Error: ABDMHealthIDViewSet::get_new_linking_token failed to fetch modes. Reason: {e}", + exc_info=True, + ) + return Response( - {"detail": "Failed to fetch modes", "error": str(e)}, + { + "detail": "Failed to fetch modes", + }, status=status.HTTP_400_BAD_REQUEST, ) @@ -463,8 +479,15 @@ def add_care_context(self, request, *args, **kwargs): } ) except Exception as e: + logger.warning( + f"Error: ABDMHealthIDViewSet::add_care_context failed. Reason: {e}", + exc_info=True, + ) + return Response( - {"detail": "Failed to add care context", "error": str(e)}, + { + "detail": "Failed to add care context", + }, status=status.HTTP_400_BAD_REQUEST, ) @@ -496,8 +519,13 @@ def patient_sms_notify(self, request, *args, **kwargs): } ) except Exception as e: + logger.warning( + f"Error: ABDMHealthIDViewSet::patient_sms_notify failed to send SMS. Reason: {e}", + exc_info=True, + ) + return Response( - {"detail": "Failed to send SMS", "error": str(e)}, + {"detail": "Failed to send SMS"}, status=status.HTTP_400_BAD_REQUEST, )