We use middleware to handle any exceptions that occur during production. The main idea is to save the debug html to a location in the file system that can be sent via a password-protected view, and then send a link to the generated view to those who need it.
Here is the basic implementation:
from django.conf import settings from django.core.mail import send_mail from django.views import debug import traceback import hashlib import sys class ExceptionMiddleware(object): def process_exception(self, request, exception): if isinstance(exception, Http404): return traceback = traceback.format_exc() traceback_hash = hashlib.md5(traceback).hexdigest() traceback_name = '%s.html' % traceback_hash traceback_path = os.path.join(settings.EXCEPTIONS_DIR, traceback_name) reporter = debug.ExceptionReporter(request, *sys.exc_info()) with open(traceback_path, 'w') as f: f.write(reporter.get_traceback_html().encode("utf-8")) send_mail( 'Error at %s' % request.path, request.build_absolute_uri(reverse('exception', args=(traceback_name, ))), FROM_EMAIL, TO_EMAIL, )
And submission
from django.conf import settings from django.http import HttpResponse def exception(request, traceback_name): traceback_path = os.path.join(settings.EXCEPTIONS_DIR, traceback_name) with open(traceback_path, 'r') as f: response = HttpResponse(f.read()) return response
Full middleware is tailored to our needs, but the basics exist. You should probably protect your password somehow. If you do not return Django's answer, then error handling will be deleted and will send you an email, but I will leave it to you.
source share