Skip to content

Commit

Permalink
Merge pull request #413 from Monstarrrr/fix/412_switch_errors_to_form…
Browse files Browse the repository at this point in the history
…errors

Switch error(s) in responses to formerror(s).
  • Loading branch information
purple-void authored Dec 24, 2024
2 parents 3ed638c + 6f18714 commit ef3bcd5
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,13 @@ class VoteView(APIView):

def post(self, request, post_type, post_id, vote_type):
if vote_type not in ["upvote", "downvote"]:
return Response({"error": "Invalid vote type"}, status=400)
return Response({"formErrors": {"vote": ["Invalid vote type"]}}, status=400)

try:
# Verify the post exists
post = Post.objects.get(id=post_id)
except Post.DoesNotExist:
return Response({"error": "Post not found"}, status=404)
return Response({"formErrors": {"post": ["Post not found"]}}, status=404)

# Check for existing vote
existing_vote = Vote.objects.filter(
Expand Down Expand Up @@ -648,13 +648,17 @@ def post(self, request, post_type, post_id):
try:
# Validate post type
if post_type.lower() not in [x[1] for x in POST_TYPES]:
return Response({"error": "Invalid post type"}, status=400)
return Response(
{"formErrors": {"post": ["Invalid post type"]}}, status=400
)

# Find the post
try:
post = Post.objects.get(id=post_id, type=post_type.lower())
except Post.DoesNotExist:
return Response({"error": "Post not found"}, status=404)
return Response(
{"formErrors": {"post": ["Post not found"]}}, status=404
)

if post.ownerUserId != request.user.id:
return Response(status=401)
Expand Down Expand Up @@ -720,7 +724,9 @@ def post(self, request):
{
"code": 400,
"message": "Invalid email format",
"errors": ["Please provide a valid email address"],
"formErrors": {
"email": ["Please provide a valid email address"]
},
},
status=400,
)
Expand All @@ -731,9 +737,11 @@ def post(self, request):
{
"code": 400,
"message": "Email already in use",
"errors": [
"This email is already associated with this account"
],
"formErrors": {
"email": [
"This email is already associated with this account"
]
},
},
status=400,
)
Expand All @@ -744,9 +752,11 @@ def post(self, request):
{
"code": 400,
"message": "Email already in use",
"errors": [
"This email is already associated with another account"
],
"formErrors": {
"email": [
"This email is already associated with another account"
]
},
},
status=400,
)
Expand Down Expand Up @@ -784,7 +794,7 @@ def post(self, request):
{
"code": 500,
"message": "Internal server error",
"errors": ["Unable to update email"],
"formErrors": {"email": ["Unable to update email"]},
},
status=500,
)
Expand Down

0 comments on commit ef3bcd5

Please sign in to comment.