I'm obviously whistling in the breeze here, but I'd be very grateful for some help. I've provided some more info below.
I have a view raising a specific exception I would like to catch and provide an HttpResponse based on this exception. I cannot trigger the exception middleware either as standard middleware or using decorator_from_middleware. My middleware is as follows -- its simply a sketch at present: class ExceptCatcher(object): def process_exception(self, request, exception): HttpResponse("Error encountered") I have verified that the middleware is being loaded. My attempt to load the middleware as a decorator (I'm using class-based views) is as follows: @decorator_from_middleware(ExceptCatcher) def dispatch(self, *args, **kwargs): return super(Locations, self).dispatch(*args, **kwargs) In neither case is ExceptCatcher being seen by decorators.py where it does an hasattr check on the middleware. On 15/05/12, Rory Campbell-Lange (r...@campbell-lange.net) wrote: > On further investigation it appears that the process_exception > middleware isn't being called in my case. In other words, > > /usr/lib/python2.7/dist-packages/django/utils/decorators.py: > 92 except Exception, e: > 93 import ipdb; ipdb.set_trace() > 94 if hasattr(middleware, 'process_exception'): > 95 result = middleware.process_exception(request, e) > 96 if result is not None: > 97 return result > > I'm not familiar enough with Django to work out the relationship between > base.py and decorators.py. However the middleware class above does not have > the > class I have loaded in base.py's self._exception_middleware. Should it have? > > Rory > > On 14/05/12, Rory Campbell-Lange (r...@campbell-lange.net) wrote: > > I have a custom database interface with (amongst other things) a lazy > > rowgetter. Our database provides custom exception messages when, for > > instance, > > a rate limit is exceeded. > > > > Because of the "laziness" of the rowgetter, the except clause in my view > > (shown below) is not triggered, and control falls to > > core/handlers/base.py : handle_uncaught_exception. > > > > try: > > rota = self.model.xmlapi_get_location (**kwargs) > > except DBException, e: > > code, message = [epart.strip() for epart in e.msg.split(':')] > > return HttpResponse(message, status=code, > > content_type="text/plain") > > > > I've tried the following: > > > > * The use of middleware using "process_exception" : DBException not raised > > * handler500 in urls : DBException not available > > > > Consequently I believe I need to either make my lazy rowgetter less lazy so > > that it errors earlier, or subclass handle_uncaught_exception. If the > > latter is > > feasible I'd be grateful for some advice on how to do so within my app. > > > > Rory > > > > p.s. > > An earlier version of this question was asked on Stack Overflow at > > http://stackoverflow.com/questions/10574581/reroute-djangos-handle-uncaught-exception > > -- > Rory Campbell-Lange > r...@campbell-lange.net -- Rory Campbell-Lange r...@campbell-lange.net Campbell-Lange Workshop www.campbell-lange.net 0207 6311 555 3 Tottenham Street London W1T 2AF Registered in England No. 04551928 -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.