I want to raise a custom exception that will:
- return 503 status
- not send Django admin email
I can do one of them, but not both together:
- return 503 status: by using DRF APIException, or custom exception handler to handle response, but I won't get the exception type in the logging record to filter.
- not send email: by checking exception type in a custom email handler class, but this returns 500.
Code example of 503 handling by adding a custom middleware:
class CustomMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
if isinstance(exception, MyCustomException):
return JsonResponse({"detail": "Error try later"}, status=503)
Code example to not send email:
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 sends email for any 5xx status response. When I use the middleware, I can't filter on reporter.exc_type since there's no exceptions trace (exc_info) anymore as the exception was handled in the process_exception.