I'm currently working on several projects that provide a JSON API using WebOb. Currently, however, whenever we use a webob.exc exception to return an error to the user (e.g., webob.exc.HTTPBadRequest) the body of that message is always in a content-type other than what they're expecting (HTML if they don't specify an Accept header, plain-text otherwise). There doesn't seem to be a pleasant, convenient, or simple way to make it use JSON beyond something like (the untested) following code:
import string
import webob.exc
class WSGIHTTPException(webob.exc.WSGIHTTPException):
body_template_obj = string.Template('{"code", ${status}, "message": "${body}", "title": "${title}"}'
plain_template_obj = string.Template('{"error": ${body}}')
class HTTPBadRequest(webob.exc.HTTPBadRequest, WSGIHTTPException):
pass
class HTTPUnauthored(webob.exc.HTTPBadRequest, WSGIHTTPException):
pass
# etc.
This is particularly problematic because we have to redefine all of the exceptions we want to use to doubly inherit from our new sub-classed WSGIHTTPException and the original. It also doesn't handle the fact that we have to basically copy and paste generate_response into our subclass so that we set the appropriate content-type header.
Is it too much to ask to either:
A) Add support for JSON response bodies in WSGIHTTPExceptions, or
B) Make WSGIHTTPException slightly more modular so we can only override parts we need?
Thanks in advance,
I'm currently working on several projects that provide a JSON API using WebOb. Currently, however, whenever we use a
webob.excexception to return an error to the user (e.g.,webob.exc.HTTPBadRequest) the body of that message is always in a content-type other than what they're expecting (HTML if they don't specify an Accept header, plain-text otherwise). There doesn't seem to be a pleasant, convenient, or simple way to make it use JSON beyond something like (the untested) following code:This is particularly problematic because we have to redefine all of the exceptions we want to use to doubly inherit from our new sub-classed
WSGIHTTPExceptionand the original. It also doesn't handle the fact that we have to basically copy and paste generate_response into our subclass so that we set the appropriate content-type header.Is it too much to ask to either:
A) Add support for JSON response bodies in
WSGIHTTPExceptions, orB) Make
WSGIHTTPExceptionslightly more modular so we can only override parts we need?Thanks in advance,