Since you explicitly want to return a JSON error (and not adapt a response
for the path) I can offer another strategy (which i use).
This is from memory so the code may need some tweaking. (working from home
today and don't have the source)
1. Define a custom class for JSON exceptions:
class ViewException_JSON(Exception):
def __init__(self, message, pyramid_exc):
self.message = message
self.pyramid_exc = pyramid_exc
2. Define a custom view exception:
@view_config(context=ViewException_JSON, renderer='json')
def exception_view_json(exc, request):
rval = {'status': error,
'message': exc.message, # this can be a python dict!
}
request.response.status_code = exc.pyramid_exc.code
# handle the `location` stuff
3. In your code, return/raise a wrapped exception. Both should work.
raise ViewException_JSON(HTTPFound(location=))
return ViewException_JSON(HTTPFound(location=))
Another option is to use a custom HttpFound class, and handle that with an
view_config `context` too. I've settled on wrapping the Pyramid exceptions
though, because I've found it a little bit easier in terms of maintenance
(documentation, understanding, etc)
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pylons-discuss/9060c7ee-312a-47f3-b04d-13b6039909d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.