I am trying to use global error handlers(app_errorhandler) in my APIs but I am getting some problems. I have a blueprint for errors where I define global errors defined as:
from werkzeug.exceptions import NotFound
from flask_app.errors import error_bp
@error_bp.app_errorhandler(NotFound)
def not_found_error(error):
return "Error 404", 404
and this works fine for all app routes, but not for APIs created by flask_restplus nor flask_restful(tried both). When I try to raise NotFound in them I get:
"The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. You have requested this URI [/user/info/1/] but did you mean /user/info// or /user/edit// or /user/login ?"
When I define a errorhandler in my API as:
@user_api.errorhandler(NotFound)
def new_error(error):
return "Error handler in API 404"
It does not use this errorhandler in API but the global one(why? how?), so I got a way to use app_errorhandler but this is not a solution as I do not want to define for every API an errorhandler if I have global set up. All my APIs are created using the blueprint they are in.
So my question is: How do I use the app_errorhandler in APIs without defining errorhandler in every API?
Answers can be flask_restplus or flask_restful because I have no problem with switching from one to the other. Thanks in advance.
from How to get Flask_restplus to use error handlers from app_errorhandler?
No comments:
Post a Comment