1. Daniel Greenfeld
  2. transifex

Commits

Diego Búrigo Zacarão  committed 7d952ea

Admin notification for important caught exception (#191)

- It works just like the Django built-in mechanism for admin
notification whenever an uncaught exception is raised with
DEBUG=False.
- Based BaseVCSError exceptions have a 'notify_admim' setting
that if set to True, will make Tx to send the notification
for the caught exception in some parts of the app.
- Initial mainling code from Dimitris Glezos <glezos@indifex.com>

  • Participants
  • Parent commits 405a9a8
  • Branches default

Comments (0)

Files changed (2)

File transifex/projects/views/component.py

View file
 from txcommon import notifications as txnotification
 
 from txcommon.decorators import one_perm_required_or_403
+from txcommon.exceptions import handle_exception_mainling
 from txcommon.forms import unit_sub_forms
 from txcommon.log import logger
 from txcommon.models import exclusive_fields, get_profile_or_user
 
         except BaseVCSError, e:
             if e.notify_admins:
-                pass
+                handle_exception_mainling(request, e)
             request.user.message_set.create(message=e.get_user_message())
 
     else:
             request.user.message_set.create(message=str(err))
         except BaseVCSError, e:
             if e.notify_admins:
-                pass
+                handle_exception_mainling(request, e)
             request.user.message_set.create(message=e.get_user_message())
         except StandardError, e:
             logger.debug("Error submiting translation file %s"

File transifex/txcommon/exceptions/__init__.py

View file
+import sys
+import traceback
+from django.core.mail import mail_admins
+from django.conf import settings
+from txcommon.log import logger
+
+def handle_exception_mainling(request, exception):
+    """Handle an exception if in production mode."""
+    exc_info = sys.exc_info()
+    subject, message = exception_email(request, exc_info)
+    if not settings.DEBUG:
+        logger.debug('Seding handled exception to admins.')
+        mail_admins(('%s - %s') % (subject, exception.message), message, 
+            fail_silently=True)
+
+def exception_email(request, exc_info):
+    """Format email subject and message for exception reporting."""
+    subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in 
+        settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
+    try:
+        request_repr = repr(request)
+    except:
+        request_repr = "Request repr() unavailable"
+    message = "%s\n\n%s" % (_get_traceback(exc_info), request_repr)
+    return subject, message
+
+def _get_traceback(exc_info=None):
+    """Helper function to return the traceback as a string."""
+    return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))
+
+def log_exception():
+    '''
+    Log the latest exception
+    '''
+    logger.error(traceback.format_exc())