To accomplish something similar, i use a mix of custom exceptions and 
pyramid's exception handling.

I have a class in my exceptions.py like this...

class ViewExceptionJsonForm(Exception):
    def __init__(self, ... ):
        ... setup with args, which might contain the Form, a Wrapped 
Exception, etc

In my view I would raise an error...

    @view_config(route="my_view")
    def my_view(self, req):
        ...
        if form.errors:
            raise ViewExceptionJsonForm(form=form)

And then I map it to a custom exception handler, which does everything for 
me...

@view_config(context=ViewExceptionJsonForm, renderer='json')
def exception_view__JsonForm(exc, request):
    request.response.status_code = xyz
    rval = new_rval()
    rval = {...}
    # populate rval based on `exc`
    return rval


Whenever I have an issue processing an API form, I raise this exception... 
and then the pyramid internals handle everything else. It's pretty great!

But as Michael noted, you probably have an issue with the frontend not 
having an appropriate accept header (or the request is being made for a 
non-json route/context if you're supporting stuff like that).

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/58eb16cd-42ec-4299-8d9c-f185c7671fd2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to