我正在使用 Flask-RESTful 构建 REST api 和 webargs 进行解析。
在定义资源时,我希望参数存在,所以我写了required=True
例如:
class Name(Resource):
"""Retrieve ids corresponding to given names
Input entries: String
"""
args = {
'entries' : fields.Str(required=True),
}
@use_kwargs(args)
def get(self, entries):
# HTTP method GET
result = object.find_id(entries)
return jsonify(result)
现在,
在对 API 执行单元测试时,明确未指定条目,它返回断言错误
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1363, in handle_user_exception
assert exc_value is e
AssertionError
当我检查时, exc_value 是
TypeError("ValidationError({'entries': [u'Missing data for required field.']}, status_code=422, headers={}) is not JSON serializable",)
并且 e 是
<UnprocessableEntity '422: Unprocessable Entity'>
现在,
我有这些方法来处理这个异常@app.errorhandler(500)
@app.errorhandler(验证错误)
@app.errorhandler(类型错误)
@app.errorhandler(UnprocessableEntity)
@app.errorhandler(422)
@app.errorhandler(异常)
我不知道为什么即使这也不起作用
@app.errorhandler(AssertionError)
参考这个: 链接 但无法解决
我正在使用 Flask(0.10.1) Flask-Restful(0.3.5) webargs(1.2.0)