def respond_with(gn_event_name=None, should_disconnect=False, emit_response=True):
def factory(view_func):
@wraps(view_func)
def decorator(*args, **kwargs):
tb = None
try:
status_code, data = view_func(*args, **kwargs)
except Exception as e:
environ.env.stats.incr(gn_event_name + '.exception')
tb = traceback.format_exc()
logger.error('%s: %s' % (gn_event_name, str(e)))
environ.env.capture_exception(sys.exc_info())
if should_disconnect and environ.env.config.get(ConfigKeys.DISCONNECT_ON_FAILED_LOGIN, False):
eventlet.spawn_after(seconds=1, func=_delayed_disconnect, sid=environ.env.request.sid)
return 500, str(e)
finally:
if tb is not None:
logger.exception(tb)
if status_code != 200:
logger.warning('in decorator, status_code: %s, data: %s' % (status_code, str(data)))
if should_disconnect and environ.env.config.get(ConfigKeys.DISCONNECT_ON_FAILED_LOGIN, False):
eventlet.spawn_after(seconds=1, func=_delayed_disconnect, sid=environ.env.request.sid)
# in some cases the callback is enough
if emit_response:
response_message = environ.env.response_formatter(status_code, data)
environ.env.emit(gn_event_name, response_message)
return status_code, None
return decorator
return factory
评论列表
文章目录