TRAC-Ticket on exception

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#settings.py
TRAC_URL = 'http://user:password@domain.tld/login/xmlrpc'

#middleware.py
from django.conf import settings
from django import http
from django.core import exceptions
import xmlrpclib
import sys

class TracTicketMiddleware(object):
    """ This middleware will catch exceptions and creates a ticket in an existing
    Trac environment

    To install, be sure to place this middleware near the beginning
    of the MIDDLEWARE_CLASSES setting in your settings file.
    This will make sure that it doesn't accidentally catch errors
    you were meaning to catch with other middleware.
    """
    IGNORE_EXCEPTIONS = (http.Http404, SystemExit, exceptions.PermissionDenied)

    def process_exception(self, request, exception):
        # If this is an error we don't want to hear about, just return.
        if isinstance(exception, self.IGNORE_EXCEPTIONS) or \
                exception in self.IGNORE_EXCEPTIONS:
            return
        
        try:
            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 = "{{{\n%s\n}}}\n\n{{{\n%s\n}}}" % (self._get_traceback(sys.exc_info()), request_repr)
            attrs = {
                'type': 'defect',
                'status': 'new',
                'priority': 'major',
            }
            server = xmlrpclib.ServerProxy(settings.TRAC_URL)
            server.ticket.create(subject, message, attrs)            
        except:
            pass
        
        return   

    def _get_traceback(self, exc_info=None):
        "Helper function to return the traceback as a string"
        import traceback
        return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))

More like this

  1. Firebug Lite Middleware by jfw 5 years, 6 months ago
  2. AgreementField by chrisrbennett 5 years, 10 months ago
  3. Little middleware that create a facebook user by marinho 6 years, 4 months ago
  4. middleware to capture doesnot exists exception by vbachu 9 months, 2 weeks ago
  5. FirstRun Middleware by TheMysteriousX 2 years, 11 months ago

Comments

(Forgotten your password?)