RT,官方文档看的不是很懂:RequestHandler.send_error(status_code=500,kwargs)**SendsthegivenHTTPerrorcodetothebrowser.Ifflush()hasalreadybeencalled,itisnotpossibletosendanerror,sothismethodwillsimplyterminatetheresponse.Ifoutputhasbeenwrittenbutnotyetflushed,itwillbediscardedandreplacedwiththeerrorpage.Overridewrite_error()tocustomizetheerrorpagethatisreturned.Additionalkeywordargumentsarepassedthroughtowrite_error.RequestHandler.write_error(status_code,kwargs)**Overridetoimplementcustomerrorpages.write_errormaycallwrite,render,set_header,etctoproduceoutputasusual.Ifthiserrorwascausedbyanuncaughtexception(includingHTTPError),anexc_infotriplewillbeavailableaskwargs["exc_info"].Notethatthisexceptionmaynotbethe“current”exceptionforpurposesofmethodslikesys.exc_info()ortraceback.format_exc.我理解的是send_error调用了write_error,所以写自定义错误页面的时候要自己实现write_error方法,抛出错误的时候使用send_error。那么又该在什么时候使用rasietornado.web.HTTPError呢?
2 回答
当年话下
TA贡献1890条经验 获得超9个赞
HTTPError一般是用tornado访问某个url,然后这个url不存在,或者返回一个错误码的时候用到的。fromtornado.httpclientimportHTTPClient,HTTPErrorhttp_client=HTTPClient()try:response=http_client.fetch(url,method='GET',request_timeout=120)exceptHTTPErrorashttp_error:printhttp_errorexceptExceptionase:printefinally:http_client.close()
添加回答
举报
0/150
提交
取消