From 50924aee34e018ebbfbfcd3a7c22fc1c063bf57d Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:44:26 +0530 Subject: [PATCH] Enhanced Error Handling in AssetViewSet (#1737) * Fix error handling in AssetViewSet * set HTTP_502_BAD_GATEWAY --- care/facility/api/viewsets/asset.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/care/facility/api/viewsets/asset.py b/care/facility/api/viewsets/asset.py index 0a1c423397..8b5d82bbc8 100644 --- a/care/facility/api/viewsets/asset.py +++ b/care/facility/api/viewsets/asset.py @@ -338,7 +338,7 @@ def operate_assets(self, request, *args, **kwargs): return Response({"result": result}, status=status.HTTP_200_OK) except ValidationError as e: - return Response({"message": e.detail}, status=status.HTTP_400_BAD_REQUEST) + return Response({"detail": e.detail}, status=status.HTTP_400_BAD_REQUEST) except KeyError as e: return Response( @@ -347,7 +347,12 @@ def operate_assets(self, request, *args, **kwargs): ) except APIException as e: - return Response(e.detail, e.status_code) + return Response( + { + "detail": f"Communication with the middleware failed.\nReceived status code: {e.status_code}" + }, + status=status.HTTP_502_BAD_GATEWAY, + ) except Exception as e: print(f"error: {e}")