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

Return custom error code #950

Open
royassis opened this issue Aug 15, 2022 · 1 comment
Open

Return custom error code #950

royassis opened this issue Aug 15, 2022 · 1 comment

Comments

@royassis
Copy link

Hey

I'm trying to return a custom error code as shown here:
https://flask-restful.readthedocs.io/en/latest/extending.html?highlight=errors#define-custom-error-messages
At the bottom of at the page.

Here is a snippet from my code:

from flask import Flask, Response
from flask_restful import Resource, Api, reqparse, abort

class SamplesNotFound(HTTPException):
    pass

error_dict = {
        "SamplesNotFound": {
        'message': "No samples found for these features.",
        'status': 522,
    }
}

class Index(Resource):
   def get(self):
      abort(522)

app = Flask(__name__)
api = Api(app, errors=errors.error_dict)

api.add_resource(Index, '/index')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False)

I get a 500 error code on the response instead of 522.
+
I want to get a json in the response and I get a string.

Please help

Best,
Roy

@bpandola
Copy link

bpandola commented Sep 1, 2022

Use raise SamplesNotFound() in place of abort(522).

P.S. Make sure you're passing errors_dict to the Api constructor. Your snippet has it written as errors.error_dict, which doesn't exist.

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