From 9b5779f738154d65d7b9bfc601ec5c1838e13fbe Mon Sep 17 00:00:00 2001 From: helllllllder Date: Mon, 16 Oct 2023 09:10:04 -0300 Subject: [PATCH] feat: treat exceptions --- chats/apps/api/v1/internal/projects/viewsets.py | 7 ++++++- chats/apps/api/v1/quickmessages/viewsets.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/chats/apps/api/v1/internal/projects/viewsets.py b/chats/apps/api/v1/internal/projects/viewsets.py index 228b57e3..d279b15f 100644 --- a/chats/apps/api/v1/internal/projects/viewsets.py +++ b/chats/apps/api/v1/internal/projects/viewsets.py @@ -67,7 +67,7 @@ def create(self, request, *args, **kwargs): def put( self, request, *args, **kwargs - ): # TODO: GAMBIARRA ALERT! MOVE THIS LOGIC TO THE SERIALIZER + ): # TODO: GAMBIARRA ALERT! MOVE THIS LOGIC TO THE SERIALIZER or somewhere else qs = self.queryset try: user_email = request.data["user"] @@ -87,6 +87,11 @@ def put( }, status.HTTP_400_BAD_REQUEST, ) + except Project.DoesNotExist: + return Response( + {"Detail": f"The project {request.data['project']} does not exist yet"}, + status.HTTP_404_NOT_FOUND, + ) return Response({"Detail": "Updated"}, status.HTTP_200_OK) @action(detail=False, methods=["POST", "GET"], permission_classes=[IsAuthenticated]) diff --git a/chats/apps/api/v1/quickmessages/viewsets.py b/chats/apps/api/v1/quickmessages/viewsets.py index 3e77d635..b75df891 100644 --- a/chats/apps/api/v1/quickmessages/viewsets.py +++ b/chats/apps/api/v1/quickmessages/viewsets.py @@ -45,9 +45,9 @@ def get_queryset(self, *args, **kwargs): try: project = self.request.GET.get("project") perm = self.request.user.project_permissions.get(project=project) - except (AttributeError, ObjectDoesNotExist): + except Exception as error: raise exceptions.APIException( - detail="You don't have permission to access this project" + detail=f"You don't have permission to access this project. {type(error)}: {error}" ) sectors = perm.get_sectors() return QuickMessage.objects.all().filter(