On 3/7/10 6:46 PM, Andrey Popp wrote:
> I have ideological question -- why not to handle
> webob.exc.HTTPException in router? I think it is very useful, consider
> for example the following case:
>
> I need to define some views that works with JSON encoded request body,
> so there is base class for them:
>
>      class JSONView(object):
>          def __init__(self, context, request):
>              try:
>                  request.json = json.loads(request.body)
>              except ValueError:
>                   raise webob.exc.HTTPBadRequest()
>              self.request = request
>
> So webob.exc.HTTPBadRequest will be propagated up to Router.

I don't have a 100% reasonable answer.  It's a bit of a grey area.

The most reasonable answer is that this *does* work:

       class JSONView(object):
           def __init__(self, context, request):
               try:
                   request.json = json.loads(request.body)
               except ValueError:
                    return webob.exc.HTTPBadRequest()
               self.request = request

Instances of exceptions in webob.exc are themselves responses, so they can be 
returned from a view function.

I can see the attraction in making the router able to handle a WebOb exception 
that implies a response.  Given that returning the exception instance does the 
same thing, it was just easier to document one way (e.g. in 
<http://docs.repoze.org/bfg/1.2/narr/views.html#using-a-view-callable-to-do-a-http-redirect>).

- C


_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to