-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
Integration with flask-restful & error handling #141
Comments
Minus the propagate_exceptions thing, I'm not sure of anything else that could cause problems with flask-restful right off hand. Could you provide a minimum example that I could use to troubleshoot this further? |
Thanks for the reply! A stripped back version of my code looks like...
When I make a call to this route with an expired token, the following exception is raised |
Thanks. Sorry, I've been a bit busy these past couple days, but I'll try to make time to look at this today. Cheers. |
I have been unable to duplicate this so far. I am testing with the flask 1.0 release that just came out, so maybe that changed something? Here is the code I use to test the issue: from flask import Flask
from flask_jwt_extended import JWTManager, jwt_required
from flask_restful import Api, Resource
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
api = Api(app)
jwt = JWTManager(app)
class SomeRoute(Resource):
@jwt_required
def post(self):
return {'message':'success'}, 200
api.add_resource(SomeRoute, '/some_route', endpoint = 'some_route')
if __name__ == '__main__':
app.run(debug=False) And here is the results of making an api call:
Could you take a look and see if you are still able to reproduce this, and if so what the full code and request looks like? Thanks! |
I also encountered this issue. I could not replicate it at all in development, but was getting 500s instead of 401s in production and staging. I was able to reproduce it by setting the debug setting to false in development mode, and the 500s showed up. I was able to fix it by setting |
It sounds like everything is all cleared up here. If that's not the case, please feel free to comment and re-open this issue 👍 |
I am encountering the issue, and
doesn't exactly do the trick for my setup. I'm using Flask-Restful's custom errors. It is true that setting Is there any way to work around this without requiring |
@alexding123 I am experiencing the same issue. |
I think I found a workaround to integrate custom errors. from flask import Blueprint
from flask_restful import Api as _Api
# some custom errors
class RoomDoesNotExist(Exception):
pass
CUSTOM_ERRORS = {
'RoomDoesNotExist': {
'message': "A room by that name does not exist.",
'status': 404,
},
}
class Api(_Api):
def error_router(self, original_handler, e):
""" Override original error_router to only custom errors and parsing error (from webargs)"""
error_type = type(e).__name__.split(".")[-1] # extract the error class name as a string
# if error can be handled by flask_restful's Api object, do so
# otherwise, let Flask handle the error
# the 'UnprocessableEntity' is included only because I'm also using webargs
# feel free to omit it
if self._has_fr_route() and error_type in list(CUSTOM_ERRORS) + ['UnprocessableEntity']:
try:
return self.handle_error(e)
except Exception:
pass # Fall through to original handler
return original_handler(e)
api_bp = Blueprint('api', __name__)
api = Api(api_bp, errors=CUSTOM_ERRORS) This way, flask_restful's errors are handled by flask_restful's Api object, and other errors fall through to Flask's error handler. |
@alexding123 many many thanks for your solution! That's the only way I've found so far to bypass this annoying issue |
Another solution for anyone who is hitting the 500 internal server error instead of the expected 401, 4XX error as expected when using flask_restful:
This enabled me to change from this before implementation:
To this post implementation:
|
Thanks @mxhr I will link this solution back to the main 401 errors not returned page! 👍 |
@mxhr that's the perfect fix, and much more simple. Thanks a lot! |
This is the best solutions for Flask-Restful and Flask-JWT-Extended in Production class CustomApi(_Api):
def error_router(self, original_handler, e):
return original_handler(e) and in resource you can simply add auth_blueprint = Blueprint("auth", __name__, url_prefix="/api")
auth_api = CustomApi(auth_blueprint) |
Similar to #86, errors like SignatureExpiry are returning 500 rather than 4xx errors.
However I am using flask-restful which does not have an error_handler, so the solutions in the referenced issue do not apply.
Flask-restful is briefly mentioned on the releases page here , but setting
application.config['PROPAGATE_EXCEPTIONS'] = True
didn't have any effect for me.Any insight into how I can correctly deal with error handling using flask-restful?
The text was updated successfully, but these errors were encountered: