I want to raise a custom exception that will:
I can do one of them, but not both together:
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.
Attach exc_info
to request
in CustomMiddleware
and access it in 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With