一、tornado 如何才能stop整个请求链然后去抛出错误呢?
比如说,进入了ManageHandler的get,
get在查询数据库,数据库错误了,raise了一个database的error,
接下来error handler把这个错误给接管了,抛出一个错误页面,然后结束这个请求链。
用finish()去结束的话报错。。。。。
二、也是关于请求链终止的问题。
比如说,进入了ManageHandler的get,
但是ManageHandler要在 init的时候判断下用户的权限,如果用户的权限不够,直接抛出404页面。
class ManageHandler(BaseHandler):
def __init__(self, *argc, **argkw):
super(ManageHandler, self).__init__(*argc, **argkw)
self.private_auth()
def private_auth(self):
self.user = self.get_secure_cookie("user")
self.private = self.UserModel.get_user_level_by_name(self.user)
#not login
if self.private == -1:
return
if self.private != 4:
self.render("404.html")
self.finish()
然后这样用finish也报错
[E 140321 02:06:04 iostream:357] Uncaught exception, closing connection.
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/
iostream.py", line 354, in wrapper
callback(*args)
File "/Library/Python/2.7/site-packages/tornado/
stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/Library/Python/2.7/site-packages/tornado/
stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/
httpserver.py", line 328, in _on_headers
self.request_callback(self._request)
File "/Library/Python/2.7/site-packages/tornado/
web.py", line 1651, in __call__
handler = spec.handler_class(self, request, **spec.kwargs)
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/
ManageHandler.py", line 23, in __init__
self.private_auth()
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/
ManageHandler.py", line 36, in private_auth
self.finish()
File "/Library/Python/2.7/site-packages/tornado/
web.py", line 837, in finish
self.flush(include_footers=True)
File "/Library/Python/2.7/site-packages/tornado/
web.py", line 784, in flush
for transform in self._transforms:
TypeError: 'NoneType' object is not iterable
[E 140321 02:06:04 ioloop:491] Exception in callback <functools.partial object at 0x10c63e1b0>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/
ioloop.py", line 477, in _run_callback
callback()
File "/Library/Python/2.7/site-packages/tornado/
stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/Library/Python/2.7/site-packages/tornado/
stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/
iostream.py", line 354, in wrapper
callback(*args)
File "/Library/Python/2.7/site-packages/tornado/
stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/Library/Python/2.7/site-packages/tornado/
stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/
httpserver.py", line 328, in _on_headers
self.request_callback(self._request)
File "/Library/Python/2.7/site-packages/tornado/
web.py", line 1651, in __call__
handler = spec.handler_class(self, request, **spec.kwargs)
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/
ManageHandler.py", line 23, in __init__
self.private_auth()
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/
ManageHandler.py", line 36, in private_auth
self.finish()
File "/Library/Python/2.7/site-packages/tornado/
web.py", line 837, in finish
self.flush(include_footers=True)
File "/Library/Python/2.7/site-packages/tornado/
web.py", line 784, in flush
for transform in self._transforms:
TypeError: 'NoneType' object is not iterable