Login

Message exception

Author:
marinho
Posted:
October 14, 2008
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

This exception is util when you want to raise an exception but want its message be shown as a message to the user, with no error 500 or 404 pages.

To use it, just append the middleware in the MIDDLEWARE_CLASSES setting and raises HttpMessage when necessary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from django.http import HttpResponseRedirect

class HttpMessageMiddleware(object):
    def process_exception(self, request, exception):
        if request.user.is_authenticated():
            request.user.message_set.create(message=unicode(exception))
            return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

class HttpMessage(Exception):
    pass

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

amitu (on October 15, 2008):

It can go in infinite loop. May be some query string parameter can be used to circumvent that.

#

guettli (on October 30, 2008):

This recipe catches all errors. "except HttpMessage" is missing in process_exception().

#

Please login first before commenting.