On Thu, May 17, 2012 at 9:51 AM, rorycl <r...@campbell-lange.net> wrote:
> Hi Tom
>
> Thanks for your email.
>
> On May 16, 4:14 pm, Tom Evans <tevans...@googlemail.com> wrote:
>> On Wed, May 16, 2012 at 12:32 PM, Rory Campbell-Lange
>>
>> <r...@campbell-lange.net> wrote:
>
>> > I have a view raising a specific exception I would like to catch and
>> > provide an HttpResponse based on this exception.
>>
>> If it is one view raising one specific exception, why aren't you
>> catching it in that view? Exception middleware is used to provide a
>> more generic error page, or for advanced error handling, or to handle
>> errors from a number of views. If it is just for one view, one
>> exception, it is more coherent to handle the error in the view.
>
> As noted before in this thread I have a lazy rowgetter that only
> triggers an exception
>  in the template. In other words, the except clause in my view isn't
> triggered.
>
>    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")
>
>> > My middleware is as follows -- its simply a sketch at present:
>>
>> >    class ExceptCatcher(object):
>> >        def process_exception(self, request, exception):
>> >            HttpResponse("Error encountered")
>>
>> Is this code verbatim? If so, you aren't returning the newly generated
>> HttpResponse, just creating it.
>
> I'm simply trying to trigger the exception handler in ipdb and that is
> not happening
> using either either the standard middleware or
> decorator_from_middleware
> methodologies. I guess this might be because control has passed from
> the view?
> decorators.py:handle_uncaught_exception _is_ called, but I am unable
> to interject my
> ExceptCatcher with it.
>
>> These sorts of questions should be on django-users really.
>
> Sorry if this is the wrong place to post. However there are a number
> of people on the
> web reporting that they are unable to trigger process_exception. Hence
> my query here.
>
>

Ah, you are returning a TemplateResponse? This changes things.

TemplateResponses are rendered outside of the view. Exception
middleware processes exceptions raised by the view, which this is not
- the view successfully completes and returns a valid HttpResponse
object.

I expect this may be a bug, or at least an unintended consequence.

This is all handled in django.core.handlers.base.BaseHandler. The view
is called here:

https://github.com/django/django/blob/master/django/core/handlers/base.py#L109

and if that raises an exception, Django will try to use the middleware
classes that have handle_exception() methods to generate a response.

If the returned response from the view is a template response, it is
rendered here:

https://github.com/django/django/blob/master/django/core/handlers/base.py#L133

if that raises an exception that isnt http.Http404,
exceptions.PermissionDenied or SystemExit, then it will use the
handle_uncaught_exception to handle the exception and return a
response.

I think this would need a new feature to fix. If a TemplateResponse
fails to render and throws an exception, you would want any middleware
that handles this to be passed both the exception and the
TemplateResponse that caused the exception, not just the exception
object.

I think you should raise a feature request ticket for this.

Cheers

Tom

-- 
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.

Reply via email to