New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attaching schema to exception data breaks out-of-the-box compatibility with flask-restful #496
Comments
Before we get to what changes would be necessary, I have to ask the question: is being compatible with
I took a look to understand where and why our usage of So, sure, we could just switch away from using For users who really do want to combine the two, we provide an easy path to customization: import flask
from webargs.flaskparser import parser
# this is just one of many paths, but this form
# changes the behavior of the parser object provided by `webargs`
# so it's the most similar to changing the default
@parser.error_handler
def handle_error(error, req, schema, *, error_status_code, error_headers):
status_code = error_status_code or parser.DEFAULT_VALIDATION_STATUS
try:
flask.abort(status_code)
except HTTPException as e:
e.webargs_data = {"schema": schema} # whatever you like here
e.data = {"messages": error.messages} # whatever you want flask-restful to see here
e.exc = error
raise e I can't get onboard with asking users who might be relying on the built-in handler to rewrite things without accepting that compatibility with Right now, I think that the cons outweigh the pros for trying to target that kind of compatibility. |
I guess since Flask-RestFul is discontinued, this issue is obsolete. |
When combining webargs with flask-restful (probably a quite popular combination), it is currently not possible to use the standard error handlers on both sides, because
data
instance variable of the raised exception, whiledata
into JSON and fails because of exactly that schema component.Looking at the discussion at RFC: Remove
fields
attribute from ValidationError marshmallow#840, it seems like the schema was only included to make it possible for an error handler to access thelocation
metadata of the fields - something that doesn't exist any more anyway. At the same time, the change with 6.0.0 that the location is automatically included in the "messages" structure seems to make the whole topic obsolete.So I propose to depreciate accessing
data["schema"]
in error handlers and in the next major version remove it to restore out-of-the-box integration with flask-restful.For a similar reason, I would propose to prepare removing
data["headers"]
and instead use a separate instance variable of the exceptions, so flask-restful doesn't by default include that in the JSON error output.The text was updated successfully, but these errors were encountered: