From 86ef142c22e6d8bef5b645641b8ff31e2cfa5528 Mon Sep 17 00:00:00 2001 From: ProCode2 Date: Sat, 13 Jan 2024 23:12:57 +0530 Subject: [PATCH 1/5] chore: log ABDM errors and send error message to client --- care/abdm/api/viewsets/auth.py | 18 ++++++++++++-- care/abdm/api/viewsets/healthid.py | 40 +++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/care/abdm/api/viewsets/auth.py b/care/abdm/api/viewsets/auth.py index 5625824126..bf34803120 100644 --- a/care/abdm/api/viewsets/auth.py +++ b/care/abdm/api/viewsets/auth.py @@ -1,5 +1,6 @@ import json from datetime import datetime, timedelta +import logging from django.core.cache import cache from rest_framework import status @@ -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( + {"error": "Error: Initialising ABDM Gateway failed."}, + status=status.HTTP_400_BAD_REQUEST, + ) return Response({}, status=status.HTTP_202_ACCEPTED) @@ -346,10 +356,14 @@ 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), + "error": "Error: Failed to notify", }, status=status.HTTP_400_BAD_REQUEST, ) diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index 2957f93914..e843370007 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -1,6 +1,7 @@ # ABDM HealthID APIs from datetime import datetime +import logging from drf_spectacular.utils import extend_schema from rest_framework import status @@ -32,6 +33,9 @@ from config.ratelimit import ratelimit +logger = logging.getLogger(__name__) + + # API for Generating OTP for HealthID class ABDMHealthIDViewSet(GenericViewSet, CreateModelMixin): base_name = "healthid" @@ -363,8 +367,15 @@ def link_via_qr(self, request): } ) except Exception as e: + logger.warning( + f"Error: ABDMHealthIDViewSet::link_via_qr failed to fetch nodes. Reason: {e}", + exc_info=True, + ) return Response( - {"detail": "Failed to fetch modes", "error": str(e)}, + { + "detail": "Failed to fetch modes", + "error": "Error: ABDM Gateway node fetch failed", + }, status=status.HTTP_400_BAD_REQUEST, ) @@ -419,8 +430,16 @@ def get_new_linking_token(self, request): } ) except Exception as e: + logger.warning( + f"Error: ABDMHealthIDViewSet::get_new_linking_token failed to fetch nodes. Reason: {e}", + exc_info=True, + ) + return Response( - {"detail": "Failed to fetch modes", "error": str(e)}, + { + "detail": "Failed to fetch modes", + "error": "Error: failed to fetch nodes.", + }, status=status.HTTP_400_BAD_REQUEST, ) @@ -463,8 +482,16 @@ 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", + "error": "Error: Failed to integrate care.", + }, status=status.HTTP_400_BAD_REQUEST, ) @@ -496,8 +523,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", "error": "Error: SMS not sent."}, status=status.HTTP_400_BAD_REQUEST, ) From c9fb8e9686fb190aac0bb8608d33a80fcb33a5dc Mon Sep 17 00:00:00 2001 From: ProCode2 Date: Sat, 13 Jan 2024 23:34:56 +0530 Subject: [PATCH 2/5] chore: pre-commit --- care/abdm/api/viewsets/auth.py | 2 +- care/abdm/api/viewsets/healthid.py | 3 +-- data/medibase.json | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/care/abdm/api/viewsets/auth.py b/care/abdm/api/viewsets/auth.py index bf34803120..50c18183b5 100644 --- a/care/abdm/api/viewsets/auth.py +++ b/care/abdm/api/viewsets/auth.py @@ -1,6 +1,6 @@ import json -from datetime import datetime, timedelta import logging +from datetime import datetime, timedelta from django.core.cache import cache from rest_framework import status diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index e843370007..32e7d35831 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -1,7 +1,7 @@ # ABDM HealthID APIs -from datetime import datetime import logging +from datetime import datetime from drf_spectacular.utils import extend_schema from rest_framework import status @@ -32,7 +32,6 @@ from config.auth_views import CaptchaRequiredException from config.ratelimit import ratelimit - logger = logging.getLogger(__name__) diff --git a/data/medibase.json b/data/medibase.json index 5b9b0cf92f..372b0b43d9 100644 --- a/data/medibase.json +++ b/data/medibase.json @@ -1144305,4 +1144305,4 @@ "cims_class_link": "/india/drug/search?q=drugs+for+erectile+dysfunction&mtype=brand&code=10d", "cims_class": "Drugs for Erectile Dysfunction", "atc_classification": "G04BE03 - sildenafil ; Belongs to the class of drugs used in erectile dysfunction." -}] \ No newline at end of file +}] From 40d5b7288029aefe2cf8a1f2edb26875f3ba27f3 Mon Sep 17 00:00:00 2001 From: ProCode2 Date: Thu, 18 Jan 2024 13:46:10 +0530 Subject: [PATCH 3/5] refactor: remove erro key as not needed, fix typo --- care/abdm/api/viewsets/auth.py | 1 - care/abdm/api/viewsets/healthid.py | 8 +++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/care/abdm/api/viewsets/auth.py b/care/abdm/api/viewsets/auth.py index 50c18183b5..79eca67bd0 100644 --- a/care/abdm/api/viewsets/auth.py +++ b/care/abdm/api/viewsets/auth.py @@ -363,7 +363,6 @@ def post(self, request, *args, **kwargs): return Response( { "detail": "Failed to notify (health-information/notify)", - "error": "Error: Failed to notify", }, status=status.HTTP_400_BAD_REQUEST, ) diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index 32e7d35831..af874da793 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -367,13 +367,12 @@ def link_via_qr(self, request): ) except Exception as e: logger.warning( - f"Error: ABDMHealthIDViewSet::link_via_qr failed to fetch nodes. Reason: {e}", + f"Error: ABDMHealthIDViewSet::link_via_qr failed to fetch modes. Reason: {e}", exc_info=True, ) return Response( { "detail": "Failed to fetch modes", - "error": "Error: ABDM Gateway node fetch failed", }, status=status.HTTP_400_BAD_REQUEST, ) @@ -430,14 +429,13 @@ def get_new_linking_token(self, request): ) except Exception as e: logger.warning( - f"Error: ABDMHealthIDViewSet::get_new_linking_token failed to fetch nodes. Reason: {e}", + f"Error: ABDMHealthIDViewSet::get_new_linking_token failed to fetch modes. Reason: {e}", exc_info=True, ) return Response( { "detail": "Failed to fetch modes", - "error": "Error: failed to fetch nodes.", }, status=status.HTTP_400_BAD_REQUEST, ) @@ -528,7 +526,7 @@ def patient_sms_notify(self, request, *args, **kwargs): ) return Response( - {"detail": "Failed to send SMS", "error": "Error: SMS not sent."}, + {"detail": "Failed to send SMS"}, status=status.HTTP_400_BAD_REQUEST, ) From a0b306bda1d2332041f6618a8652fa33abaa86b6 Mon Sep 17 00:00:00 2001 From: ProCode2 Date: Thu, 18 Jan 2024 19:09:46 +0530 Subject: [PATCH 4/5] undo changes in data/mediabase.json --- data/medibase.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/medibase.json b/data/medibase.json index 372b0b43d9..5b9b0cf92f 100644 --- a/data/medibase.json +++ b/data/medibase.json @@ -1144305,4 +1144305,4 @@ "cims_class_link": "/india/drug/search?q=drugs+for+erectile+dysfunction&mtype=brand&code=10d", "cims_class": "Drugs for Erectile Dysfunction", "atc_classification": "G04BE03 - sildenafil ; Belongs to the class of drugs used in erectile dysfunction." -}] +}] \ No newline at end of file From 8335ba9388e6603b975b5c1372adb0c3314f81f7 Mon Sep 17 00:00:00 2001 From: ProCode2 Date: Thu, 18 Jan 2024 19:18:17 +0530 Subject: [PATCH 5/5] refactor: return error messages properly --- care/abdm/api/viewsets/auth.py | 2 +- care/abdm/api/viewsets/healthid.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/care/abdm/api/viewsets/auth.py b/care/abdm/api/viewsets/auth.py index 79eca67bd0..5802a493db 100644 --- a/care/abdm/api/viewsets/auth.py +++ b/care/abdm/api/viewsets/auth.py @@ -33,7 +33,7 @@ def post(self, request, *args, **kwargs): exc_info=True, ) return Response( - {"error": "Error: Initialising ABDM Gateway failed."}, + {"detail": "Error: Initialising ABDM Gateway failed."}, status=status.HTTP_400_BAD_REQUEST, ) diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index af874da793..318fa7e3b7 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -487,7 +487,6 @@ def add_care_context(self, request, *args, **kwargs): return Response( { "detail": "Failed to add care context", - "error": "Error: Failed to integrate care.", }, status=status.HTTP_400_BAD_REQUEST, )