Skip to content
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

Is expected behavior of flask_restful abort() correct ? #955

Open
mikehagerty opened this issue Oct 18, 2022 · 1 comment
Open

Is expected behavior of flask_restful abort() correct ? #955

mikehagerty opened this issue Oct 18, 2022 · 1 comment

Comments

@mikehagerty
Copy link

Hi.
Sorry to post this here - completely new to flask-restful, but ...

From the docs, I expected that flask-restful arg_parse() would return
an http page with error code (e.g., 400) + any error messages accumulated
while parsing the query string.

However, the error messages do not appear.

I can see that the error messages are accumulating correctly in
reqparse.py (e.g., handle_validation_error with bundle_errors=True).

When I look at the flask-restful.abort(), I don't understand how this is meant to work:
Inside__init__.py:

def abort(http_status_code, **kwargs):
    """Raise a HTTPException for the given http_status_code. Attach any keyword
    arguments to the exception for later processing.
    """
    #noinspection PyUnresolvedReferences
    try:
        original_flask_abort(http_status_code)
    except HTTPException as e:
        if len(kwargs):
            e.data = kwargs
        raise

To me, it looks like you return an original flask abort page with no message, and then raise the flask-restful error message as an exception. How is this meant to work ?

e.g., if I just try to do:
import flask_restful
flask_restful.abort(400, message={'field1': 'this is required', 'field2': 'got a bad value'})

My understanding from the docs is that this should be all that is required to return html with error messages, but it does not.

Thanks!!

@sgeorgevv
Copy link

The abort function in Flask-RESTful is meant to provide an enhanced version of Flask's abort function by allowing you to attach custom error messages or any other keyword arguments to the raised HTTPException.

Here's how it works:

  • When you call abort(http_status_code, **kwargs), it first tries to invoke Flask's built-in abort function with the provided http_status_code.

  • Flask's abort raises an HTTPException for the given status code.

  • The except HTTPException as e block catches this raised exception.

  • If any keyword arguments were passed (like custom error messages), they are attached to the data attribute of the caught exception.

  • The exception (now with the attached data) is raised again, and Flask-RESTful catches it and processes it to return a response with the custom error messages.

The problem you're facing might be because of how your application is set up. Here are some things to check:

  • Check for other error handlers: If you have other error handlers in your Flask application (using @app.errorhandler), they might interfere with Flask-RESTful's error handling mechanism. Ensure these handlers return the error messages you expect.

  • Ensure bundle_errors is set: If you're using Flask-RESTful's reqparse to parse request data and want to bundle all error messages together, ensure you've set bundle_errors=True in your Api instance:

api = Api(app, bundle_errors=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants