This commit is contained in:
Your Name
2026-02-14 16:50:02 +08:00
parent 0ac1e9549c
commit 2e143fb0c0
14 changed files with 588 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""
DRF 自定义异常处理器:统一错误响应格式。
"""
from rest_framework.views import exception_handler
from rest_framework.response import Response
def custom_exception_handler(exc, context):
response = exception_handler(exc, context)
if response is not None:
# 统一格式:{"detail": "..."}
if isinstance(response.data, list):
response.data = {"detail": response.data[0] if response.data else str(exc)}
elif isinstance(response.data, dict) and "detail" not in response.data:
response.data = {"detail": str(response.data)}
return response