【发布时间】:2014-02-20 18:41:45
【问题描述】:
我在 python-eve 应用程序中有一些代码,它从设备检索一些数据并在第一次请求该资源时填充资源。有时代码无法成功连接到设备。在这种情况下,我想返回一条错误消息来更好地解释这一点,而不仅仅是一个简单的 500 错误。这是 on_fetch_item 钩子:
def before_returning_item(resource, _id, document):
if resource == "switches":
if "interfaces" not in document.keys():
# retrieve and store switch config if it hasn't been stored yet
r = app.data.driver.db[resource]
try:
switch = prepare_switch_from_document(document)
except socket.timeout:
# raise some more meaningful error with message
pass
interface_list = switch.get_formatted_interfaces()
r.update({'_id': _id}, {'interfaces': interface_list})
document['interfaces'] = interface_list
app.on_fetch_item += before_returning_item
提前致谢。
【问题讨论】:
-
你有一些奇怪的格式