簡體   English   中英

AppEngine / Python:為什么不抓住異常?

[英]AppEngine/Python: Why isn't the exception caught?

我正在嘗試編寫一個Google-Appengine應用程序,當數據存儲區寫入被禁用時,該應用程序將很好地失敗

目前我的main()看起來像這樣:

def main():
    make_datastore_readonly()
    try:
        run_wsgi_app(application)
    except CapabilityDisabledError:
        run_wsgi_app(NoWrite)


如果我將main設置為:

 def main(): run_wsgi_app(application) 

我的應用程序在引發異常時顯示回溯。


如果我將main設置為:

 def main(): run_wsgi_app(NoWrite) 

它將正確顯示我的錯誤消息(盡管對於每個請求)。


回到我修改過的main版本,這個版本:

def make_datastore_readonly():
  """Throw ReadOnlyError on put and delete operations."""
  def hook(service, call, request, response):
    assert(service == 'datastore_v3')
    if call in ('Put', 'Delete'):
      raise CapabilityDisabledError('Datastore is in read-only mode') //Line 18
  apiproxy_stub_map.apiproxy.GetPreCallHooks().Push('readonly_datastore', hook, 'datastore_v3')

我得到的回溯看起來像這樣,而不是收到我的錯誤信息:

 Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__ handler.post(*groups) File "/Users/kevin/Sche/main.py", line 232, in post me.put(); File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 1074, in put return datastore.Put(self._entity, **kwargs) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/datastore.py", line 579, in Put return PutAsync(entities, **kwargs).get_result() File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/datastore.py", line 556, in PutAsync return _GetConnection().async_put(config, entities, local_extra_hook) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1553, in async_put return make_put_call(base_req, pbs, extra_hook) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1543, in make_put_call self.__put_hook, user_data) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1188, in make_rpc_call rpc.make_call(method, request, response, get_result_hook, user_data) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 519, in make_call self.__service, method, request, response, self.__rpc) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 207, in Call function(service, call, request, response) File "/Users/kevin/Sche/main.py", line 18, in hook raise CapabilityDisabledError('Datastore is in read-only mode') CapabilityDisabledError: Datastore is in read-only mode 

所以,我的問題是,為什么不抓住例外?

編輯:

此函數來自此StackOverflow答案

 def make_datastore_readonly(): """Throw ReadOnlyError on put and delete operations.""" def hook(service, call, request, response): assert(service == 'datastore_v3') if call in ('Put', 'Delete'): raise CapabilityDisabledError('Datastore is in read-only mode') //Line 18 apiproxy_stub_map.apiproxy.GetPreCallHooks().Push('readonly_datastore', hook, 'datastore_v3') 

main函數只注冊此應用程序。 因此,在main函數中不會引發異常。 因此try ... catch語句將不起作用。

處理此異常的方法是定義新的RequestHandler。 然后,所有想要擁有此功能的請求都應該來自新的RequestHandler。

例如:

Class MyRequestHandler(RequestHandler):
    def get(self):
        try:
            self.get_handler()
        except CapabilityDisabledError:
            pass

class MyRequest(MyRequestHandler):
    def get_handler(self):
        # ....
        pass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM