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)?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/EtPHiu7SIB8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.