哈哈
This commit is contained in:
17
server/core/exception_handler.py
Normal file
17
server/core/exception_handler.py
Normal 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
|
||||
Reference in New Issue
Block a user