Login

TRAC-Ticket on exception

Author:
identify
Posted:
July 10, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

This is a small approach to have a middleware which automatically creates a ticket in an existing Trac environment.

Note: you must have the XML-RPC-Plugin installed.

Extend the attrs-dict to your needs. For example: in my case I have the SensitiveTicket-Plugin installed - automatically created tickets are marked as sensitive and are not visible to the public.

 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:[email protected]/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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.