Ticket #11565: error_emails_rate_limit.patch
File error_emails_rate_limit.patch, 3.6 KB (added by , 15 years ago) |
---|
-
django/core/handlers/base.py
1 try: 2 from hashlib import md5 3 except ImportError: 4 from md5 import md5 1 5 import sys 2 6 7 from django.core.cache import cache 3 8 from django import http 4 9 from django.core import signals 5 10 from django.utils.encoding import force_unicode … … 154 159 return debug.technical_500_response(request, *exc_info) 155 160 156 161 # When DEBUG is False, send an error message to the admins. 157 subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path) 162 # To ensure we only send one email per ERROR_EMAIL_RATE_LIMIT time period 163 # we take an md5 of the traceback and store it in the cache with a timeout. 164 # If the value exists in the cache then we skip sending an email. 165 tb = self._get_traceback(exc_info) 166 key = "django_error_md5_%s" % (md5(unicode(tb).encode("utf8")).hexdigest(), ) 167 168 # The worst case is for the cache to have failed and to be raising exceptions so we mask them all. 158 169 try: 159 request_repr = repr(request)170 cache_hit = cache.get(key) is not None 160 171 except: 161 request_repr = "Request repr() unavailable" 162 message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr) 163 mail_admins(subject, message, fail_silently=True) 172 cache_hit = False 173 174 if not cache_hit: 175 subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path) 176 try: 177 request_repr = repr(request) 178 except: 179 request_repr = "Request repr() unavailable" 180 message = "%s\n\n%s" % (tb, request_repr) 181 mail_admins(subject, message, fail_silently=True) 182 183 # If the error emails are being rate limited set a cache value to prevent the same email being send again 184 if hasattr(settings, "ERROR_EMAIL_RATE_LIMIT") and settings.ERROR_EMAIL_RATE_LIMIT > 0: 185 try: 186 cache.set(key, "Error Occurred!", settings.ERROR_EMAIL_RATE_LIMIT*60) 187 except: 188 pass 189 164 190 # Return an HttpResponse that displays a friendly error message. 165 191 callback, param_dict = resolver.resolve500() 166 192 return callback(request, **param_dict) -
docs/ref/settings.txt
447 447 448 448 Whether to use a TLS (secure) connection when talking to the SMTP server. 449 449 450 .. setting:: ERROR_EMAIL_RATE_LIMIT 451 452 ERROR_EMAIL_RATE_LIMIT 453 ---------------------- 454 455 Default: ``0`` 456 457 How often to send emails to the admins regarding errors in minutes. If set to 0 an email is send for every exception, if greater than 0 then an email is send once every ``ERROR_EMAIL_RATE_LIMIT`` minutes for each distinct traceback. 458 459 This settings requires a configured cache backend to function correctly. 460 450 461 .. setting:: FILE_CHARSET 451 462 452 463 FILE_CHARSET