Skip to content

Commit

Permalink
fix: add specific field to exception return
Browse files Browse the repository at this point in the history
  • Loading branch information
kallilsouza committed Dec 23, 2024
1 parent f9d541b commit bf52532
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion connect/api/v2/organizations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_cannot_create_organization_project_with_invalid_name_length(
response, content_data = self.request(path, method, user=user, data=data)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["name"][0].code, "max_length")
self.assertEqual(response.data["organization"]["name"][0].code, "max_length")

@patch("connect.internals.event_driven.producer.rabbitmq_publisher.RabbitmqPublisher.send_message")
@patch("connect.authentication.models.User.send_request_flow_user_info")
Expand Down
16 changes: 13 additions & 3 deletions connect/api/v2/organizations/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pendulum
from rest_framework import mixins, status
from rest_framework import mixins, status, exceptions
from rest_framework.decorators import action
from rest_framework.viewsets import GenericViewSet
from rest_framework.response import Response
Expand Down Expand Up @@ -83,7 +83,12 @@ def create(self, request, *args, **kwargs):

# Organization
serializer = self.get_serializer(data=org_data)
serializer.is_valid(raise_exception=True)

try:
serializer.is_valid(raise_exception=True)
except exceptions.ValidationError as e:
raise exceptions.ValidationError({"organization": e.detail}, code=e.get_codes())

instance = serializer.save()

if type(instance) == dict:
Expand All @@ -94,7 +99,12 @@ def create(self, request, *args, **kwargs):
{"organization": instance.uuid}
)
project_serializer = ProjectSerializer(data=project_data, context={"request": request})
project_serializer.is_valid(raise_exception=True)

try:
project_serializer.is_valid(raise_exception=True)
except exceptions.ValidationError as e:
raise exceptions.ValidationError({"project": e.detail}, code=e.get_codes())

project_instance = project_serializer.save()

if type(project_instance) == dict:
Expand Down

0 comments on commit bf52532

Please sign in to comment.