Skip to content

Instantly share code, notes, and snippets.

@RRRoger
Created September 21, 2018 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RRRoger/7827bec0c5979aa0ccac3f45bdb16fc2 to your computer and use it in GitHub Desktop.
Save RRRoger/7827bec0c5979aa0ccac3f45bdb16fc2 to your computer and use it in GitHub Desktop.
装饰器 抓取接口异常 创建接口日志
def try_except_log(func):
"""
# 供参考
装饰器:
1.抓取异常
2.创建日志
"""
def wrapper(*args, **kw):
response = False
is_success = False
try:
response = func(*args, **kw)
except Exception, e:
request.env.cr.rollback()
_logger.error(u"[API.ERROR]:%s, rollback" % str(e))
response = _response(ERROR_CODE_SYS[0] % str(e), ERROR_CODE_SYS[1])
finally:
data = response.data
res = json.loads(data)
if res['code'] == 0:
is_success = True
create_val = {
'request_body': str(kw),
'response_body': data,
'is_success': is_success,
'call_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'api_name': func.__name__
}
get_model('hs.api.log').create(create_val)
return response
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment