Skip to content

Commit

Permalink
Refactor custom exception handler to format errors consistently using…
Browse files Browse the repository at this point in the history
… SuccessResponse
  • Loading branch information
QuvonchbekBobojonov committed Dec 11, 2024
1 parent a74ae05 commit 3187398
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="django-success-response",
version="1.0.3",
version="1.0.5",
description="Django app for customizing response",
author="Quvonchbek Bobojonov",
author_email="[email protected]",
Expand Down
13 changes: 7 additions & 6 deletions src/success_response/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ def success_exception_handler(exc, context):
if response is not None:
data = response.data

# Ensure the response data is a dictionary for consistent formatting.
if not isinstance(data, dict):
# Extract the first error message if the data isn't already a dictionary.
data = {'detail': ''.join(i for i in data)}
# Check if 'detail' key exists, otherwise format error messages.
if 'detail' not in data:
# If 'detail' is missing, combine all error messages into a single string.
# For each key in data, join associated errors into a string.
data = {'detail': ' '.join(f"{key}: {' '.join(errors)}" for key, errors in data.items())}

# Wrap the error response in a standardized format.
# Wrap the error response in a standardized format using SuccessResponse.
response = SuccessResponse(data, success=False)
else:
# Return a generic internal server error response if no response was created.
# If DRF doesn't provide a response, return a generic internal server error response.
response = SuccessResponse(
{'detail': 'Internal Server Error'},
success=False,
Expand Down

0 comments on commit 3187398

Please sign in to comment.