def server_exception_wrap(func):
"""
Method decorator to return nicer JSON responses: handle internal server errors & report request timings.
"""
@wraps(func)
def _wrapper(self, *args, **kwargs):
try:
# append "success=1" and time taken in milliseconds to the response, on success
logger.debug("calling server method '%s'" % (func.func_name))
cherrypy.response.timeout = 3600 * 24 * 7 # [s]
# include json data in kwargs; HACK: should be a separate decorator really, but whatever
if getattr(cherrypy.request, 'json', None):
kwargs.update(cherrypy.request.json)
start = time.time()
result = func(self, *args, **kwargs)
if result is None:
result = {}
result['success'] = 1
result['taken'] = time.time() - start
logger.info("method '%s' succeeded in %ss" % (func.func_name, result['taken']))
return result
except Exception, e:
logger.exception("exception serving request")
result = {
'error': repr(e),
'success': 0,
}
cherrypy.response.status = 500
return result
return _wrapper
w2v_server.py 文件源码
python
阅读 19
收藏 0
点赞 0
评论 0
评论列表
文章目录