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
This commit is contained in:
pablohashescobar 2023-04-18 12:25:22 +05:30 committed by GitHub
parent be5ef61428
commit 2b280935a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,9 @@
# Python imports # Python imports
from itertools import groupby from itertools import groupby
# Django imports
from django.db import IntegrityError
# Third party imports # Third party imports
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
@ -36,6 +39,22 @@ class StateViewSet(BaseViewSet):
.distinct() .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): def list(self, request, slug, project_id):
try: try:
state_dict = dict() state_dict = dict()