Skip to content

Commit

Permalink
ABDM: Log the errors instead of sending them as a response (#1821)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ProCode2 and sainak authored Feb 10, 2024
1 parent 4136005 commit 5ed5c38
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
17 changes: 15 additions & 2 deletions care/abdm/api/viewsets/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
from datetime import datetime, timedelta

from django.core.cache import cache
Expand All @@ -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,)
Expand All @@ -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)

Expand Down Expand Up @@ -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,
)
Expand Down
36 changes: 32 additions & 4 deletions care/abdm/api/viewsets/healthid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ABDM HealthID APIs

import logging
from datetime import datetime

from drf_spectacular.utils import extend_schema
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down

0 comments on commit 5ed5c38

Please sign in to comment.