I would like to chime in because I have a similar problem. When a user
requests an image in my application that isn't available then I'd like to
return a dummy image along with an HTTP response code 404. But as soon as I
set the status to 404 or even abort(404) then no matter what I try I always
get an HTTP response. Overriding functions isn't really my preferred way
here either. It's one of the few occassions where the framework got in my
way. :)
Cheers
Christoph
I've made a json_abort function to raise customized webob.exc in my lib/util.py:
import simplejson as json
def json_abort(status_code=None, detail="", headers=None, comment=None):
"""Same with pylons.controllers.util.abort but with json output."""
exc_class = status_map[status_code]
class JsonException(exc_class):
def __call__(self, environ, start_response):
self.content_type = 'text/javascript; charset=utf-8'
self.body = json.dumps(self.detail)
return Response.__call__(self, environ, start_response)
exc = JsonException(detail=detail, headers=headers, comment=comment)
log.debug("Aborting request with JSON response, status: %s,
detail: %r, headers: %r, "
"comment: %r", status_code, detail, headers, comment)
request.environ['pylons.status_code_redirect'] = True # skip err doc
raise exc.exception
so that, I can just call json_abort(404, {'reason': "resource not
found"}) in my controllers and users will receive a 404 error response
with a json documenting the reason in it.
--
Qiangning Hong
http://www.douban.com/people/hongqn/