Returning JSON error when catching Django exception?

时间秒杀一切 提交于 2019-12-07 12:16:22

问题


Does anyone have opinions on the best way of having middleware catch exceptions, and instead of rendering the error into a HTML template, to return a JSON object? Currently I have the middleware below that catches exceptions, and if it can find an extra user error message, puts that onto the request (that the template then picks up).

class ExceptionUserErrorMessageMiddleware(object):
    def process_exception(self, request, exception):
        """ if the exception has information relevant to the user, then 
        tack that onto the request object"""

        theFormat = djrequest.get_getvar(request, settings.FORMAT_PARAM, "")
        msg = getMessage(exception)
        if msg:
            setattr(request, USER_ERROR_MESSAGE_ATTR, msg)

        if theFormat == "json":
            print "do something"

What's the best way of returning a json object here? Should I set any additional headers?

Is there a way of doing the same for exceptional circumstances that don't pass through middleware (I'm pretty sure 404 doesn't, are there any others)?


回答1:


What we do on our project MapIt - https://github.com/mysociety/mapit - for doing this is to raise an Exception to a middleware, as you say, which then directly returns either an HTML template or a JSON object depending on the format provided by the request. You can view the code at https://github.com/mysociety/mapit/blob/master/mapit/middleware/view_error.py

In terms of additional headers, our output_json function sets the Content-Type on the response to 'application/json; charset=utf-8'.

For the 404 case, we have our own get_object_or_404 function that wraps get_object_or_404 and converts a Django Http404 exception into our own exception that will then correctly return JSON if appropriate to the request.

from django.shortcuts import get_object_or_404 as orig_get_object_or_404

def get_object_or_404(klass, format='json', *args, **kwargs):
    try:
        return orig_get_object_or_404(klass, *args, **kwargs)
    except http.Http404, e:
        raise ViewException(format, str(e), 404)


来源:https://stackoverflow.com/questions/11129079/returning-json-error-when-catching-django-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!