I am using Flask-RESTful for building REST api and webargs for parsing.
While defining resource, I want argument to be present, so I wrote required=True
For example:
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)
now,
While performing unittesting for API, explicitly not specifying entries, it returns Assertion Error
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
When I checked, exc_value is
TypeError("ValidationError({'entries': [u'Missing data for required field.']}, status_code=422, headers={}) is not JSON serializable",)
and e is
<UnprocessableEntity '422: Unprocessable Entity'>
Now,
I've these ways to handle this exception @app.errorhandler(500)
@app.errorhandler(ValidationError)
@app.errorhandler(TypeError)
@app.errorhandler(UnprocessableEntity)
@app.errorhandler(422)
@app.errorhandler(Exception)
I don't know why even this is not working
@app.errorhandler(AssertionError)
Refered to this: link but wasn't able to solve
I am using Flask(0.10.1) Flask-Restful(0.3.5) webargs(1.2.0)