最近我在使用 webargs(8.0.1) 和 flask-restful(0.3.8) 时遇到了一些问题
当我尝试使用时异常抛出两次cURL http://127.0.0.1:5001/?score=100A
我认为它应该返回带有函数的 jsonfy 错误消息,handle_request_parsing_error
但它只返回 HTTP 500 错误。
希望有人能帮我解决这个问题,非常感谢
from flask import Flask
from flask_restful import Api, Resource
from webargs import fields, validate
from webargs.flaskparser import use_args, use_kwargs, parser, abort
app = Flask(__name__)
api = Api(app)
class ScoreResource(Resource):
"""A welcome page."""
score_args = {"score": fields.Int(required=True)}
@use_args(score_args, location="query")
def get(self, args):
return {"message": "You score is {}!".format(args["score"])}
# This error handler is necessary for usage with Flask-RESTful
@parser.error_handler
def handle_request_parsing_error(err, req, schema):
"""webargs error handler that uses Flask-RESTful's abort function to return
a JSON error response to the client.
"""
abort(422, errors=err.messages)
if __name__ == "__main__":
api.add_resource(ScoreResource, "/")
app.run(port=5001, debug=True)
127.0.0.1 - - [10/Sep/2021 18:05:33] "GET /?score=100A HTTP/1.1" 500 -
Traceback (most recent call last):
File "E:\projects\tools\venv\lib\site-packages\webargs\core.py", line 328, in parse
data = schema.load(preprocessed_data, **load_kwargs)
File "E:\projects\tools\venv\lib\site-packages\marshmallow\schema.py", line 720, in load
data, many=many, partial=partial, unknown=unknown, postprocess=True
File "E:\projects\tools\venv\lib\site-packages\marshmallow\schema.py", line 901, in _do_load
raise exc
marshmallow.exceptions.ValidationError: {'score': ['Not a valid integer.']}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "E:\projects\tools\venv\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
return original_handler(e)
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\projects\tools\venv\lib\site-packages\flask\_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "E:\projects\tools\venv\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
return original_handler(e)
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "E:\projects\tools\venv\lib\site-packages\flask\_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "E:\projects\tools\venv\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\projects\tools\venv\lib\site-packages\flask_restful\__init__.py", line 468, in wrapper
resp = resource(*args, **kwargs)
File "E:\projects\tools\venv\lib\site-packages\flask\views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "E:\projects\tools\venv\lib\site-packages\flask_restful\__init__.py", line 583, in dispatch_request
resp = meth(*args, **kwargs)
File "E:\projects\tools\venv\lib\site-packages\webargs\core.py", line 445, in wrapper
error_headers=error_headers,
File "E:\projects\tools\venv\lib\site-packages\webargs\core.py", line 337, in parse
error_headers=error_headers,
File "E:\projects\tools\venv\lib\site-packages\webargs\core.py", line 236, in _on_validation_error
error_headers=error_headers,
TypeError: handle_request_parsing_error() got an unexpected keyword argument 'error_status_code'