From 2b280935a1756e87f64c5c088aa7baa9b97c567e Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Tue, 18 Apr 2023 12:25:22 +0530 Subject: [PATCH] chore: update state create endpoint to send error response on integrity error (#869) * chore: update state create endpoint to send error response on integrity error * dev: update status code for general exception --- apiserver/plane/api/views/state.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apiserver/plane/api/views/state.py b/apiserver/plane/api/views/state.py index 4616fcee7f0..048ac4a6ad0 100644 --- a/apiserver/plane/api/views/state.py +++ b/apiserver/plane/api/views/state.py @@ -1,6 +1,9 @@ # Python imports from itertools import groupby +# Django imports +from django.db import IntegrityError + # Third party imports from rest_framework.response import Response from rest_framework import status @@ -36,6 +39,22 @@ class StateViewSet(BaseViewSet): .distinct() ) + def create(self, request, slug, project_id): + try: + serializer = StateSerializer(data=request.data) + if serializer.is_valid(): + serializer.save(project_id=project_id) + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + except IntegrityError: + return Response( + {"error": "State with the name already exists"}, + status=status.HTTP_400_BAD_REQUEST, + ) + except Exception as e: + capture_exception(e) + return Response({"error": "Something went wrong please try again later"}, status=status.HTTP_400_BAD_REQUEST) + def list(self, request, slug, project_id): try: state_dict = dict()