Files
boss_dp/server/core/exception_handler.py

18 lines
643 B
Python
Raw Normal View History

2026-02-14 16:50:02 +08:00
# -*- 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