def exception_handler_mist(exc, request):
"""
Here we catch exceptions and transform them to proper http responses
This is a special pyramid view that gets triggered whenever an exception
is raised from any other view. It catches all exceptions exc where
isinstance(exc, context) is True.
"""
# mongoengine ValidationError
if isinstance(exc, me.ValidationError):
trace = traceback.format_exc()
log.warning("Uncaught me.ValidationError!\n%s", trace)
return Response("Validation Error", 400)
# mongoengine NotUniqueError
if isinstance(exc, me.NotUniqueError):
trace = traceback.format_exc()
log.warning("Uncaught me.NotUniqueError!\n%s", trace)
return Response("NotUniqueError", 409)
# non-mist exceptions. that shouldn't happen! never!
if not isinstance(exc, MistError):
if not isinstance(exc, (me.ValidationError, me.NotUniqueError)):
trace = traceback.format_exc()
log.critical("Uncaught non-mist exception? WTF!\n%s", trace)
return Response("Internal Server Error", 500)
# mist exceptions are ok.
log.info("MistError: %r", exc)
# if it is a RedirectError, then send an HTTP Redirect
if isinstance(exc, RedirectError):
return HTTPFound(exc.url or '')
# else translate it to HTTP response based on http_code attribute
return Response(str(exc), exc.http_code)
评论列表
文章目录