使用 `abort(404)` 时会引发 TypeError
TypeError is raised when using `abort(404)`
当我使用abort(status_code=404, detail='No such user', passthrough='json')
引发此异常:
TypeError: 'NoneType' object is not iterable
这是回溯:
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/appwrappers/identity.py", line 47, in __call__
return self.next_handler(controller, environ, context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/appwrappers/i18n.py", line 71, in __call__
return self.next_handler(controller, environ, context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/wsgiapp.py", line 285, in _dispatch
return controller(environ, context)
File "/home/jugger/workspace/web/ave/ave/lib/base.py", line 27, in __call__
return TGController.__call__(self, environ, context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/dispatcher.py", line 119, in __call__
response = self._perform_call(context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/dispatcher.py", line 108, in _perform_call
r = self._call(action, params, remainder=remainder, context=context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/decoratedcontroller.py", line 125, in _call
response = self._render_response(context, controller, output)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/decoratedcontroller.py", line 220, in _render_response
for name in exclude_names:
TypeError: 'NoneType' object is not iterable
--------------------- >> end captured logging << ---------------------
这是我的代码:
我试图获取一个不存在的帐户,因此 NoResultFound
被捕获,结果 abort
必须完成。但它引发了我上面提到的异常。
@expose('json')
def get_one(self, account_id):
"""
Get an account
:param account_id :type: str
:return Account :type: dict
"""
try:
_id = int(account_id)
except ValueError:
abort(status_code=400, detail='account_id must be int', passthrough='json')
try:
account = DBSession.query(Account).filter(Account.id == _id).one()
except NoResultFound:
abort(status_code=404, detail='No such user', passthrough='json')
return dict(
id=account.id,
username=account.username,
reputation=account.reputation,
badges=account.badges,
created=account.created,
bio=account.bio
)
这就是身份验证层所做的事情,每当向用户发出需要身份验证的信号时,挑战者就会进行干预并强制用户登录 (http://turbogears.readthedocs.io/en/latest/turbogears/authentication.html?highlight=challenger#how-it-works-in-turbogears)
如果你想避免这种行为,最简单的方法是使用 tg.abort(401, passthrough=True)
这将跳过这一步,因为你正在谈论一个 API 你可能想使用 passthrough='json'
这将提供 JSON 响应。参见 http://turbogears.readthedocs.io/en/latest/reference/classes.html#tg.controllers.util.abort
您的回复可能会被 ErrorPageApplicationWrapper
捕获,具体取决于 TurboGears 版本,在这种情况下请确保 ErrorController.document
具有 @expose('json')
,否则您将面临您提到的崩溃。
当我使用abort(status_code=404, detail='No such user', passthrough='json')
引发此异常:
TypeError: 'NoneType' object is not iterable
这是回溯:
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/appwrappers/identity.py", line 47, in __call__
return self.next_handler(controller, environ, context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/appwrappers/i18n.py", line 71, in __call__
return self.next_handler(controller, environ, context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/wsgiapp.py", line 285, in _dispatch
return controller(environ, context)
File "/home/jugger/workspace/web/ave/ave/lib/base.py", line 27, in __call__
return TGController.__call__(self, environ, context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/dispatcher.py", line 119, in __call__
response = self._perform_call(context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/dispatcher.py", line 108, in _perform_call
r = self._call(action, params, remainder=remainder, context=context)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/decoratedcontroller.py", line 125, in _call
response = self._render_response(context, controller, output)
File "/home/jugger/.virtualenvs/ave/lib/python3.5/site-packages/tg/controllers/decoratedcontroller.py", line 220, in _render_response
for name in exclude_names:
TypeError: 'NoneType' object is not iterable
--------------------- >> end captured logging << ---------------------
这是我的代码:
我试图获取一个不存在的帐户,因此 NoResultFound
被捕获,结果 abort
必须完成。但它引发了我上面提到的异常。
@expose('json')
def get_one(self, account_id):
"""
Get an account
:param account_id :type: str
:return Account :type: dict
"""
try:
_id = int(account_id)
except ValueError:
abort(status_code=400, detail='account_id must be int', passthrough='json')
try:
account = DBSession.query(Account).filter(Account.id == _id).one()
except NoResultFound:
abort(status_code=404, detail='No such user', passthrough='json')
return dict(
id=account.id,
username=account.username,
reputation=account.reputation,
badges=account.badges,
created=account.created,
bio=account.bio
)
这就是身份验证层所做的事情,每当向用户发出需要身份验证的信号时,挑战者就会进行干预并强制用户登录 (http://turbogears.readthedocs.io/en/latest/turbogears/authentication.html?highlight=challenger#how-it-works-in-turbogears)
如果你想避免这种行为,最简单的方法是使用 tg.abort(401, passthrough=True)
这将跳过这一步,因为你正在谈论一个 API 你可能想使用 passthrough='json'
这将提供 JSON 响应。参见 http://turbogears.readthedocs.io/en/latest/reference/classes.html#tg.controllers.util.abort
您的回复可能会被 ErrorPageApplicationWrapper
捕获,具体取决于 TurboGears 版本,在这种情况下请确保 ErrorController.document
具有 @expose('json')
,否则您将面临您提到的崩溃。