Flask-restful: Abort(404) appends to message

Created on 1 Oct 2018  路  3Comments  路  Source: flask-restful/flask-restful

Hello,
I'm new to using flask_restful, and I love it so far, however, I'm wondering if I'm missing something obvious when it comes to the abort(404) function.

I'd like to specify my own message for an abort(404). It adds my message, but appends additional information to the message I specified. This isn't what I was expecting. When I looked through the code I only seen a way to turn this off globally.

Steps to reproduce:

app = Flask(__name__)
api = Api(app)


def abort_account_doesnt_exist(account_id):
    abort(404, message="Account {} doesn't exist".format(account_id))


class AccountList(Resource):
    def get(self):
        abort_account_doesnt_exist(123)


api.add_resource(AccountList, '/accounts')

This produces the following error message:

{"message": "Account 123 doesn't exist. You have requested this URI [/accounts] but did you mean /accounts or /accounts/<account_id> or /generate_account ?"}

Because I specified a message on the abort, I expected just my result.

{"message": "Account 123 doesn't exist."}

Looking through the code to try and make this stop happening, I found the following in flask_restful/__init__.py:

        help_on_404 = current_app.config.get("ERROR_404_HELP", True)
        if code == 404 and help_on_404:
            rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
                          for rule in current_app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path, rules.keys())
            if close_matches:
                # If we already have a message, add punctuation and continue it.
                if "message" in data:
                    data["message"] = data["message"].rstrip('.') + '. '
                else:
                    data["message"] = ""

                data['message'] += 'You have requested this URI [' + request.path + \
                                   '] but did you mean ' + \
                                   ' or '.join((
                                       rules[match] for match in close_matches)
                                   ) + ' ?'

Where current_app is defined globally. I like the default message when I haven't specified one, but where I specify it, It is my opinion that it should not add additional information to my string by default.

Is there something I am missing?

Most helpful comment

You need to set ERROR_404_HELP False

app.config['ERROR_404_HELP'] = False

image

All 3 comments

You need to set ERROR_404_HELP False

app.config['ERROR_404_HELP'] = False

image

The 404 help feature has been removed and this should not happen whenever a new release is made

Thank you for the input. Still Loving this package and all the effort everyone has put into it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbpasker picture rbpasker  路  4Comments

n0trace picture n0trace  路  3Comments

makmanalp picture makmanalp  路  8Comments

skavie picture skavie  路  5Comments

BenjaVR picture BenjaVR  路  4Comments