如何处理Python错误:tornado.application:未捕获的异常

时间:2016-04-28 08:00:13

标签: python error-handling tornado

我有以下代码一直触发ERROR:tornado.application:Uncaught exception GET而我似乎无法弄清楚如何正确处理它?<​​/ p>

class MainHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self, id):
        http = tornado.httpclient.AsyncHTTPClient()
        endpoint = "https://www.foobar.com/api/v1"
        foo = yield http.fetch("{}/foo/{}".format(endpoint, id), self._handle_fetch)
        bar = yield http.fetch("{}/bar/{}".format("http://www.doh.com", id), self._handle_fetch)
        // do other things at this point like serve content to user

def _handle_fetch(self, response):
    if response.error:
        print("there was an error so returning None")
        return {}
    else:
        print("everything OK so returning response")
        return response

当柱线收益率失败时,foo收益率将成功

如何处理异常,以便在条形图产​​量失败时我可以继续向用户提供内容? (因为我不关心它是否成功)

更新

我设法处理错误:

    try:
        foo = yield http.fetch("{}/foo/{}".format(endpoint, id), self._handle_fetch)
        bar = yield http.fetch("{}/bar/{}".format("http://www.doh.com", id), self._handle_fetch)
        print(foo.body)
        print(bar.body)
    except tornado.httpclient.HTTPError as e:
        print("Error: " + str(e))
    except Exception as e:
        print("Error: " + str(e))

但我正在尝试找出如何识别哪个 fetch导致失败,因为我想尝试在foo成功并且{{bar fails. But if时仍然提供内容1}} foo`失败我想让整个响应失败并发送自定义错误

1 个答案:

答案 0 :(得分:0)

好的,我设法解决了这个问题:

protected