最新版生产环境后端

This commit is contained in:
XingQue
2026-06-12 03:43:54 +08:00
commit 25d6ec70f3
197 changed files with 74825 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
# /opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django/utils/exception_handler.py
from rest_framework.views import exception_handler
from rest_framework.response import Response
from rest_framework import status
import logging
import os
logger = logging.getLogger('django')
def custom_exception_handler(exc, context):
"""
生产环境安全异常处理器 - 不泄露任何敏感信息
基于你之前项目的代码,做了小优化
"""
# 先调用默认异常处理器
response = exception_handler(exc, context)
if response is not None:
# 🔥 记录原始错误信息到日志(便于排查)
request = context['request']
user_info = f"用户: {request.user.id if request.user.is_authenticated else '未登录'}"
logger.error(
f"API异常 - 路径: {request.path}, "
f"方法: {request.method}, {user_info}, "
f"状态码: {response.status_code}, 错误: {str(exc)}"
)
# 🔥 生产环境返回标准化错误,不暴露任何细节
error_messages = {
status.HTTP_400_BAD_REQUEST: {'code': 400, 'message': '请求参数错误'},
status.HTTP_401_UNAUTHORIZED: {'code': 401, 'message': '请先登录'},
status.HTTP_403_FORBIDDEN: {'code': 403, 'message': '权限不足'},
status.HTTP_404_NOT_FOUND: {'code': 404, 'message': '资源不存在'},
status.HTTP_405_METHOD_NOT_ALLOWED: {'code': 405, 'message': '请求方法不允许'},
status.HTTP_408_REQUEST_TIMEOUT: {'code': 408, 'message': '请求超时'},
status.HTTP_429_TOO_MANY_REQUESTS: {'code': 429, 'message': '请求过于频繁'},
status.HTTP_500_INTERNAL_SERVER_ERROR: {'code': 500, 'message': '服务器内部错误'},
status.HTTP_502_BAD_GATEWAY: {'code': 502, 'message': '网关错误'},
status.HTTP_503_SERVICE_UNAVAILABLE: {'code': 503, 'message': '服务暂不可用'},
status.HTTP_504_GATEWAY_TIMEOUT: {'code': 504, 'message': '网关超时'},
}
# 🔥 如果是调试模式,返回详细错误(便于开发)
from django.conf import settings
if settings.DEBUG:
return response
# 🔥 生产环境返回标准错误
standard_response = error_messages.get(
response.status_code,
{'code': response.status_code, 'message': '请求错误'}
)
return Response(standard_response, status=response.status_code)
# 对于非DRF异常这里可以根据需要处理
return response