section 2

This commit is contained in:
IGNY8 VPS (Salman)
2025-11-14 16:15:18 +00:00
parent 66b1868672
commit d14d6093e0
2 changed files with 129 additions and 93 deletions

View File

@@ -56,16 +56,9 @@ def extract_error_message(exc, response):
if isinstance(exc.detail, dict):
# Validation errors - use as errors dict
errors = exc.detail
# Extract first error message as top-level error
if errors:
first_key = list(errors.keys())[0]
first_error = errors[first_key]
if isinstance(first_error, list) and first_error:
error_message = f"{first_key}: {first_error[0]}"
else:
error_message = f"{first_key}: {first_error}"
else:
error_message = "Validation failed"
# Set a general validation error message
# The specific field errors are in the errors dict
error_message = "Validation failed"
elif isinstance(exc.detail, list):
# List of errors
error_message = exc.detail[0] if exc.detail else "An error occurred"
@@ -76,14 +69,35 @@ def extract_error_message(exc, response):
elif response and hasattr(response, 'data'):
# Try to extract from response data
if isinstance(response.data, dict):
# Check for common error message fields
error_message = (
response.data.get('error') or
response.data.get('message') or
response.data.get('detail') or
str(response.data)
)
errors = response.data if 'error' not in response.data else None
# If response already has unified format, preserve it
if 'success' in response.data:
error_message = response.data.get('error', 'An error occurred')
errors = response.data.get('errors')
return error_message, errors
# If response.data looks like validation errors (dict with field names as keys)
# and no 'error', 'message', or 'detail' fields, treat it as validation errors
if 'error' not in response.data and 'message' not in response.data and 'detail' not in response.data:
# This is a validation error dict - extract errors and set error message
errors = response.data
error_message = 'Validation failed'
else:
# Check for common error message fields
error_message = (
response.data.get('error') or
response.data.get('message') or
response.data.get('detail') or
'Validation failed' if response.data else 'An error occurred'
)
# Extract errors from response.data if it's a dict with field errors
# Check if response.data contains field names (likely validation errors)
if isinstance(response.data, dict) and any(
isinstance(v, (list, str)) for v in response.data.values()
) and 'error' not in response.data and 'message' not in response.data:
errors = response.data
error_message = 'Validation failed'
else:
errors = response.data if 'error' not in response.data else None
elif isinstance(response.data, list):
error_message = response.data[0] if response.data else "An error occurred"
errors = {"non_field_errors": response.data}
@@ -148,6 +162,15 @@ def custom_exception_handler(exc, context):
# Determine status code
if response is not None:
status_code = response.status_code
# If response already has unified format (success field), return it as-is
# This handles cases where error_response() was manually returned
if hasattr(response, 'data') and isinstance(response.data, dict):
if 'success' in response.data:
# Response already in unified format - just add request_id if needed
if request_id and 'request_id' not in response.data:
response.data['request_id'] = request_id
return response
else:
# Unhandled exception - default to 500
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
@@ -188,8 +211,13 @@ def custom_exception_handler(exc, context):
}
# Add errors dict if present
# Always include errors if we have them, even if error_message was set
if errors:
error_response_data["errors"] = errors
# If we have errors but no error message was set, ensure we have one
elif not error_message or error_message == "An error occurred":
error_message = get_status_code_message(status_code)
error_response_data["error"] = error_message
# Add request ID for error tracking
if request_id: