Skip to content

Commit 547febf

Browse files
author
libertyzhu
committed
optimize exception retry logs
1 parent 1ab4d05 commit 547febf

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

qcloud_cos/cos_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, ci_req
359359
kwargs['verify'] = False
360360
if self._conf._allow_redirects is not None:
361361
kwargs['allow_redirects'] = self._conf._allow_redirects
362+
exception_logbuf = list() # 记录每次重试的错误日志
362363
for j in range(self._retry + 1):
363364
try:
364365
if j != 0:
@@ -397,13 +398,16 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, ci_req
397398
else:
398399
break
399400
except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
400-
logger.exception('url:%s, retry_time:%d exception:%s' % (url, j, str(e)))
401+
# 记录每次请求的exception
402+
exception_log = 'url:%s, retry_time:%d exception:%s' % (url, j, str(e))
403+
exception_logbuf.append(exception_log)
401404
if j < self._retry and (isinstance(e, ConnectionError) or isinstance(e, Timeout)): # 只重试网络错误
402405
if client_can_retry(file_position, **kwargs):
403406
if not domain_switched and self._conf._auto_switch_domain_on_retry and self._conf._ip is None:
404407
url = switch_hostname_for_url(url)
405408
domain_switched = True
406409
continue
410+
logger.exception(exception_logbuf) # 最终重试失败, 输出前几次重试失败的exception
407411
raise CosClientError(str(e))
408412

409413
if not cos_request:
@@ -419,12 +423,16 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, ci_req
419423
if 'x-cos-trace-id' in res.headers:
420424
info['traceid'] = res.headers['x-cos-trace-id']
421425
logger.warn(info)
426+
if len(exception_logbuf) > 0:
427+
logger.exception(exception_logbuf) # 最终重试失败, 输出前几次重试失败的exception
422428
raise CosServiceError(method, info, res.status_code)
423429
else:
424430
msg = res.text
425431
if msg == u'': # 服务器没有返回Error Body时 给出头部的信息
426432
msg = res.headers
427433
logger.error(msg)
434+
if len(exception_logbuf) > 0:
435+
logger.exception(exception_logbuf) # 最终重试失败, 输出前几次重试失败的exception
428436
raise CosServiceError(method, msg, res.status_code)
429437

430438
return None

0 commit comments

Comments
 (0)