Skip to content

Commit

Permalink
Add exception handler for validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
samarpan1738 committed Dec 22, 2024
1 parent 39d1972 commit 4220a2c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions todo/exceptions/exception_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import exception_handler
from rest_framework.utils.serializer_helpers import ReturnDict

from todo.dto.responses.error_response import ApiErrorDetail, ApiErrorResponse, ApiErrorSource


def handle_exception(exc, context):
if isinstance(exc, ValidationError):
return Response(
ApiErrorResponse(
statusCode=status.HTTP_400_BAD_REQUEST,
message="Invalid request",
errors=format_validation_errors(exc.detail),
).model_dump(mode="json", exclude_none=True),
status=status.HTTP_400_BAD_REQUEST,
)
return exception_handler(exc, context)


def format_validation_errors(errors) -> List[ApiErrorDetail]:
formatted_errors = []
if isinstance(errors, ReturnDict | dict):
for field, messages in errors.items():
if isinstance(messages, list):
for message in messages:
formatted_errors.append(ApiErrorDetail(detail=message, source={ApiErrorSource.PARAMETER: field}))
elif isinstance(messages, dict):
nested_errors = format_validation_errors(messages)
formatted_errors.extend(nested_errors)
else:
formatted_errors.append(ApiErrorDetail(detail=messages, source={ApiErrorSource.PARAMETER: field}))
return formatted_errors
1 change: 1 addition & 0 deletions todo_project/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
"rest_framework.renderers.JSONRenderer",
],
"UNAUTHENTICATED_USER": None,
"EXCEPTION_HANDLER": "todo.exceptions.exception_handler.handle_exception",
}

0 comments on commit 4220a2c

Please sign in to comment.