python - “连接意外关闭”没有错误类型。怎么抓?
问题描述
在尝试将大量报告从 API 流式传输到 Bigquery 时,我的 App Engine 应用偶尔会遇到一个我不知道如何捕捉的错误。
我已经在处理urlfetch_errors.InternalTransientError
和urlfetch_errors.DeadlineExceededError
.
while True:
try:
result = service.tabledata().insertAll(
projectId=svcdata['project_id'],
datasetId=cfg.DATASET_ID,
tableId=convert_table_id(site),
body=insert_all_data).execute(num_retries=cfg.STREAM_RETRIES)
break
except (urlfetch_errors.InternalTransientError, urlfetch_errors.DeadlineExceededError):
log.info("An transient error occured while streaming to BQ. Retrying in 30 secs.")
time.sleep(30)
通常我使用错误的类型DeadlineExceededError
来捕获错误。然而,这个错误只是“错误”。
Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/f~stat-data-logging/20190801t112501.420000215685215810/controllers/cron.py", line 19, in get
runStatDownload()
File "/base/data/home/apps/f~stat-data-logging/20190801t112501.420000215685215810/utils/utils_stat.py", line 138, in runStatDownload
streamResult = stream_rows_to_bigquery(data, report.title)
File "/base/data/home/apps/f~stat-data-logging/20190801t112501.420000215685215810/utils/utils_bigq.py", line 165, in stream_rows_to_bigquery
body=insert_all_data).execute(num_retries=cfg.STREAM_RETRIES)
File "./lib/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "./lib/googleapiclient/http.py", line 846, in execute
method=str(self.method), body=self.body, headers=self.headers)
File "./lib/googleapiclient/http.py", line 164, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "./lib/oauth2client/transport.py", line 159, in new_request
credentials._refresh(orig_request_method)
File "./lib/oauth2client/client.py", line 749, in _refresh
self._do_refresh_request(http)
File "./lib/oauth2client/client.py", line 780, in _do_refresh_request
body=body, headers=headers)
File "./lib/oauth2client/transport.py", line 282, in request
connection_type=connection_type)
File "./lib/httplib2/__init__.py", line 2135, in request
cachekey,
File "./lib/httplib2/__init__.py", line 1796, in _request
conn, request_uri, method, body, headers
File "./lib/httplib2/__init__.py", line 1737, in _conn_request
response = conn.getresponse()
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_dist/lib/python2.7/gae_override/httplib.py", line 536, in getresponse
'An error occured while connecting to the server: %s' % e)
error: An error occured while connecting to the server: Connection closed unexpectedly by server at URL: https://oauth2.googleapis.com/token
我想将堆栈跟踪中显示的错误添加到我的错误处理中。做这个的最好方式是什么?
解决方案
我认为您可以执行以下操作:
while True:
try:
result = service.tabledata().insertAll(
projectId=svcdata['project_id'],
datasetId=cfg.DATASET_ID,
tableId=convert_table_id(site),
body=insert_all_data).execute(num_retries=cfg.STREAM_RETRIES)
break
except (urlfetch_errors.InternalTransientError, urlfetch_errors.DeadlineExceededError):
log.info("An transient error occured while streaming to BQ. Retrying in 30 secs.")
time.sleep(30)
except:
print "Unexpected error:", sys.exc_info()[0]
raise
推荐阅读
- regression - 调整滚动回归
- python - 将数据保存为 SequenceExample 并读取 LSTM 的 TFRecords
- android - 如何在 android 的 TextureView 上显示 SurfaceTextures
- javafx - Gluon Scene Builder 16.0.0 无法在 Windows 10 64 位中启动
- reactjs - 如何从另一个文件中的反应 axios 函数返回 const?
- html - 在具有线性渐变背景的元素上使用边框,导致边缘出现纯色
- python - Python:根据用户输入向函数传递参数并执行函数
- asp.net-mvc - 安全默认值不适用于 Azure AD 图形 API 创建的用户
- angular - Uglify、Minify 和压缩 Angular 12 库
- jmeter - 在 JMeter 请求正文中获取当前 UTC 时间戳