首页 > 解决方案 > Django 自定义异常返回 503 并跳过发送管理员电子邮件

问题描述

我想提出一个自定义异常,它将:

我可以做其中之一,但不能同时做:

通过添加自定义中间件处理 503 的代码示例:

class CustomMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        if isinstance(exception, MyCustomException):
            return JsonResponse({"detail": "Error try later"}, status=503)

不发送电子邮件的代码示例:

class CustomAdminEmailHandler(AdminEmailHandler):
    def emit(self, record):
        ...
        reporter = ExceptionReporter(request, is_email=True, *exc_info)
        if reporter.exc_type and issubclass(reporter.exc_type, MyCustomException):
            return

Django 为任何 5xx 状态响应发送电子邮件。当我使用中间件时,我无法过滤,reporter.exc_type因为不再有异常跟踪(exc_info),因为异常是在 process_exception 中处理的。

标签: djangoexceptiondjango-rest-framework

解决方案


附加exc_inforequestinCustomMiddleware并在 中访问它CustomAdminEmailHandler

class CustomMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        if isinstance(exception, MyCustomException):
            # For CustomAdminEmailHandler      # Add this
            request.exc_info = sys.exc_info()  # Add this
            return JsonResponse({"detail": "Error try later"}, status=503)
class CustomAdminEmailHandler(log.AdminEmailHandler):
    def emit(self, record):
        request = record.request
        if record.exc_info:
            exc_info = record.exc_info
        elif hasattr(request, 'exc_info'):  # Add this
            # From CustomMiddleware         # Add this
            exc_info = request.exc_info     # Add this
        else:
            exc_info = (None, record.getMessage(), None)
        reporter = ExceptionReporter(request, is_email=True, *exc_info)
        if reporter.exc_type and issubclass(reporter.exc_type, MyCustomException):
            return

推荐阅读