Yes, I know I can make it work by returning webob.exc.HTTPException
objects as response. But the point was to decode JSON request at view
initialization phase and raise webob.exc.HTTPBadRequest if needed, so
in subclasses at request processing phase (__call__ method) I always
will have well-formed request objects.

By looking at the Router code it seems that handling
repoze.bfg.exceptions.NotFound/Forbidden is some kind of bonus feature
for views, because it is more intended for serving
and context finding/view lookup mechanisms, isn't it?

> 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
> <>).
Repoze-dev mailing list

Reply via email to