Blog entries on March 2008

  • Django error notification with jabber

    By Nuno Mariz, on 7 March 2008 @ 13:26
    Django has a code error notifications mechanism when a view raises an exception. It will email the people in ADMIN tuple(settings documentation) in with the full exception information and displays the default 500.html template.
    This only happens when DEBUG=False in the

    It's possible to set a handle that change this behavior with a handler500 variable in the root
    So, we can easily write a simple view that sends an error notification to our jabber account.

    First of all, you need to install xmpppy and dnspython:

    $ easy_install xmpppy
    $ easy_install dnspython

    Add to the jabber parameters such as the jabber id, password, recipient, etc.:

    JABBER_ID = ''
    JABBER_PASSWORD = 'your_jabber_password'
    JABBER_ERROR_TEXT = 'An error occurred in "Project Name", please check your email.'

    Start a new app named errors or something else and add it to the INSTALLED_APPS tuple in the

    python startapp errors

    Add a handler500 variable with the view in the root

    handler500 = 'errors.views.server_error_jabber'

    Finally add the view in errors.views that sends a jabber notification and returns a 500 error page:

    from django.views.defaults import server_error
    from django.conf import settings
    import xmpp, time
    def server_error_jabber(request, template_name='500.html'):
           jid = xmpp.protocol.JID(settings.JABBER_ID)
           cl = xmpp.Client(jid.getDomain(), debug=[])
           conn = cl.connect()
           if conn:
               auth = cl.auth(jid.getNode(), settings.JABBER_PASSWORD, resource=jid.getResource())
               if auth:
                   id = cl.send(xmpp.protocol.Message(settings.JABBER_RECIPIENT, settings.JABBER_ERROR_TEXT))
                   # Some older servers will not send the message if you disconnect immediately after sending
       return server_error(request, template_name)

    NOTE: Don't forget to set DEBUG=False in the