简体   繁体   English

AppEngine / Python:为什么不抓住异常?

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

I'm trying to write a Google-Appengine app that will fail nicely when datastore writes are disabled 我正在尝试编写一个Google-Appengine应用程序,当数据存储区写入被禁用时,该应用程序将很好地失败

Currently my main() looks like this: 目前我的main()看起来像这样:

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


If I set main to: 如果我将main设置为:

 def main(): run_wsgi_app(application) 

My app displays a traceback when the exception is raised. 我的应用程序在引发异常时显示回溯。


If I set main to: 如果我将main设置为:

 def main(): run_wsgi_app(NoWrite) 

It will properly show my error message (although for every request). 它将正确显示我的错误消息(尽管对于每个请求)。


Getting back to my modified version of main, this one: 回到我修改过的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')

Instead of getting my error message, I still get a traceback that looks like this: 我得到的回溯看起来像这样,而不是收到我的错误信息:

 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 

So, my question is, why isn't the exception caught? 所以,我的问题是,为什么不抓住例外?

Edit: 编辑:

This function is from this StackOverflow answer 此函数来自此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') 

the main function only register this application. main函数只注册此应用程序。 Therefore, the exception will not raise in the main function. 因此,在main函数中不会引发异常。 Therefore the try ... catch statement won't work. 因此try ... catch语句将不起作用。

The way to handle this exception is defining a new RequestHandler. 处理此异常的方法是定义新的RequestHandler。 Then, all requests which want to have this feature should inherent from the new RequestHandler. 然后,所有想要拥有此功能的请求都应该来自新的RequestHandler。

for example: 例如:

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