From 8fda06e2b152a8c1633732f01eae199135b7b958 Mon Sep 17 00:00:00 2001 From: Happy Felix Chukwuma Date: Fri, 29 Nov 2024 14:33:26 +0100 Subject: [PATCH] update article create/update status code --- server/apps/research/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/apps/research/views.py b/server/apps/research/views.py index 3e4f174..1667139 100644 --- a/server/apps/research/views.py +++ b/server/apps/research/views.py @@ -43,6 +43,8 @@ def create(self, request, *args, **kwargs): return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception as e: logger.error(f"Unexpected error during article creation: {e}") + if isinstance(e, serializers.ValidationError): + return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) return Response({'error': 'Failed to create a new Article'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def update(self, request, *args, **kwargs): @@ -53,8 +55,10 @@ def update(self, request, *args, **kwargs): serializer.is_valid(raise_exception=True) self.perform_update(serializer) return Response(serializer.data, status=status.HTTP_200_OK) - except Exception as e: + except Exception as e: logger.error(f"Unexpected error during article update: {e}") + if isinstance(e, serializers.ValidationError): + return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) return Response({'error': 'Error updating article'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Custom action to retrieve articles by slug or UUID