gpt4 book ai didi

python - 应用引擎/ python : Why isn't the exception caught?

转载 作者:行者123 更新时间:2023-11-28 17:49:58 27 4
gpt4 key购买 nike

我正在尝试编写一个 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 main():
make_datastore_readonly()
try:
run_wsgi_app(application)
except CapabilityDisabledError:
run_wsgi_app(NoWrite)

我没有收到错误消息,而是得到如下所示的回溯:

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

所以,我的问题是,为什么没有捕获异常?

编辑:

这个函数来自this StackOverflow answer

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 函数只注册这个应用程序。因此,不会在主函数中引发异常。因此 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

关于python - 应用引擎/ python : Why isn't the exception caught?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12020350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com