I have some code in a python-eve app that retrieves some data from a device and populates a resource when that resource is requested for the first time. Sometimes the code can't successfully connect to the device. In this case, I would like to return an error message that explains this better, rather than just a plain 500 error. Here is the on_fetch_item hook:
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
Thanks in advance.