0

我最近在try-catch塊中包含了一個函數調用,以捕獲all URL Fetch異常,但根據下面的日誌,它沒有被捕獲。DeadlineExceededError not catch in try catch

from google.appengine.api import urlfetch_errors 
:: 
:: 
try: 
    gitkit_user  = gitkit_instance.VerifyGitkitToken (self.request.cookies['gtoken']) 
except (urlfetch_errors.DeadlineExceededError, urlfetch_errors.ConnectionClosedError, urlfetch_errors.DNSLookupFailedError, urlfetch_errors.DownloadError, urlfetch_errors.Error, urlfetch_errors.InternalTransientError, urlfetch_errors.InvalidMethodError, urlfetch_errors.InvalidURLError, urlfetch_errors.MalformedReplyError, urlfetch_errors.PayloadTooLargeError, urlfetch_errors.ResponseTooLargeError, urlfetch_errors.SSLCertificateError, urlfetch_errors.TooManyRedirectsError): 

    logging.error ("Exception occurred while executing gitkit_instance.VerifyGitkitToken(). \nDetails: "+traceback.format_exc()) 

錯誤日誌,包括跟蹤

The API call urlfetch.Fetch() took too long to respond and was cancelled. 
Traceback (most recent call last): 
    File "/base/data/home/runtimes/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/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/data/home/runtimes/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/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/base/data/home/runtimes/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/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 1057, in add_context_wrapper 
    return synctaskletfunc(*args, **kwds) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 1038, in synctasklet_wrapper 
    return taskletfunc(*args, **kwds).get_result() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 1011, in tasklet_wrapper 
    result = func(*args, **kwds) 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/main_v3.py", line 29956, in get 
    gitkit_user  = gitkit_instance.VerifyGitkitToken (self.request.cookies['gtoken']) 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/gitkitclient.py", line 216, in VerifyGitkitToken 
    certs = self.rpc_helper.GetPublicCert() 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/rpchelper.py", line 159, in GetPublicCert 
    resp, content = self.http.request(cert_url, headers=headers) 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/httplib2/__init__.py", line 1570, in request 
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/httplib2/__init__.py", line 1317, in _request 
    (response, content) = self._conn_request(conn, request_uri, method, body, headers) 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/httplib2/__init__.py", line 1286, in _conn_request 
    response = conn.getresponse() 
    File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/gae_override/httplib.py", line 522, in getresponse 
    **extra_kwargs) 
    File "/base/data/home/apps/s~myapp/1.389163610985502634/httplib2/__init__.py", line 1089, in fixed_fetch 
    validate_certificate=validate_certificate) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 271, in fetch 
    return rpc.get_result() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result 
    return self.__get_result_hook(self) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 378, in _get_fetch_result 
    rpc.check_success() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 579, in check_success 
    self.__rpc.CheckSuccess() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_rpc.py", line 134, in CheckSuccess 
    raise self.exception 
DeadlineExceededError: The API call urlfetch.Fetch() took too long to respond and was cancelled. 

我使用谷歌應用程序引擎和Python。
我做錯了什麼?

+3

原始錯誤可能被GIT代碼捕獲並被替換。添加另一個「Exception Exception」子句來捕獲所有其他異常,並顯示異常詳細信息以嘗試識別到達代碼的實際異常。 –

+0

@DanCornilescu謝謝。我添加了一個額外的'Exception Exception'子句。我會等待異常再次發生。 – gsinha

+0

我使用:from google.appengine.runtime import DeadlineExceededError – voscausa

回答

1

這可能不是完全回答你的問題,但爲什麼不能做這樣的事情:

try: 
    gitkit_user = gitkit_instance.VerifyGitkitToken (self.request.cookies['gtoken']) 
except Exception as e: 
    logging.error ("Exception occurred while executing gitkit_instance.VerifyGitkitToken(). \nDetails: " + e) 

錯誤將捕獲和錯誤的類型將印(E)。

+0

謝謝。我添加了一個額外的'Exception Exception'子句。我會等待異常再次發生。 – gsinha