Tornado中的`send_error`, `write_error`和`raise HTTPError`有什么区别?

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呢?
牧羊人nacy
浏览 894回答 2
2回答

当年话下

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()

忽然笑

send_error是Torando发送错误状态码到浏览器,而write_error是对自定义的错误页面进行重写,是2个不同的操作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript