New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for JSON Exception Bodies #209

Closed
sigmavirus24 opened this Issue Jul 26, 2015 · 1 comment

Comments

Projects
None yet
2 participants
@sigmavirus24
Contributor

sigmavirus24 commented Jul 26, 2015

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,

@bertjwregeer

This comment has been minimized.

Show comment
Hide comment
@bertjwregeer

bertjwregeer Jul 26, 2015

Member

It's definitely not too much to ask. It's a good idea!

Member

bertjwregeer commented Jul 26, 2015

It's definitely not too much to ask. It's a good idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment