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 settings.py with the full exception information and displays the default 500.html template.
This only happens when DEBUG=False in the settings.py.

It's possible to set a handle that change this behavior with a handler500 variable in the root urls.py.
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 settings.py the jabber parameters such as the jabber id, password, recipient, etc.:

JABBER_ERROR_NOTIFICATION = True
JABBER_ID = 'your_jabberid@jabberdomain.com'
JABBER_PASSWORD = 'your_jabber_password'
JABBER_RECIPIENT = 'recipient@jabberdomain.com'
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 settings.py:

python manage.py startapp errors

Add a handler500 variable with the view in the root urls.py:

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'):
   if settings.JABBER_ERROR_NOTIFICATION:
       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
               time.sleep(1)
   return server_error(request, template_name)

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

Comments

  • #1 By Jannis Leidel on 7 March 2008 @ 14:34
    Cool stuff Nuno!
  • #2 By Nuno Mariz on 7 March 2008 @ 14:48
    Thanks,
    Actually, you are responsible for this idea, after I've listened the latest "This Week in Django" podcast by Michael Trier, that talked about your Pownce Jabber Bot. ;)
  • #3 By Empty on 7 March 2008 @ 15:04
    Very cool idea. Thanks. I'll be implementing this today.
  • #4 By Pedro Lima on 7 March 2008 @ 16:38
    I'm sure I will be using it. Thanks.
  • #5 By Parand Darugar on 31 March 2008 @ 23:03
    > handler500 = 'errors.views.server_error_jabber'

    Nice, didn't know about this. Jabber integration for errors, neat idea.
  • #6 By coulix on 18 July 2008 @ 21:45
    sweet
  • #7 By Sebastian on 13 February 2009 @ 13:25
    this is saving my day!
    thanks alot
Comments are closed.