Part 4: Exception Handling
Howdy! In the previous Part of the series, we learned how we can add authentication and authorization. In thi...
For further actions, you may consider blocking this person and/or reporting abuse
very good article! thanks
please update the above link, you current references a 404 this is the new link:
flask-restful.readthedocs.io/en/la...
a minor addition:
in the new docs its says:
"Note: Custom Exceptions must have HTTPException as the base Exception."
if i do change the errors in the dictionary to extends HTTPException instead of Exception i get:
otherwise its an http 500 error and the flask debugging views ( html)
Thanks
[UPDATE]: I've found another working workaround (I think you too ;) ) : github.com/vimalloc/flask-jwt-exte...
Very nice article series! Thanks a lot for the great stuff!
One question: have you tried accessing the protected endpoints without passing the Authorization header? According to my test, it will throw a 500 instead of expected 401. It seems that the error handling framework could not catch the internal NoAuthorizationError from jwt_extended properly.
I tried one suggestion from github.com/flask-restful/flask-res... . However, it will make the situation even worse: NoAuthorizationError can be properly returned, but all other self-defined errors could not be caught. Do you have a better solution to that?
Haha glad that you found it ;) I asked the question there while writing the article and got the answer back after I have published the article.
Thanks for editing your comment to help others :)
Great Tutorials, thanks.
This exceptions didn't work to me, until i replaced "Exception" with "HTTPException".
maybe there was a change ....
see here the last line in the page:
flask-restful.readthedocs.io/en/la...
following the code the error was raised but did not get the friendly json message.. what is wrong?
I was having that trouble but if you turn debug off i.e. app.run() rather than app.run(debug=True)
it works for me
Please have a look at the GitHub link and see if you missed anything.
If it doesn't work out please let me know what you tried, and where the error occurred.
Hi. Great articles. Cant wait till you make a frontend implementation series (as promised in ;) )
How come you havn't made a "ExpiredTokenError" in errors.py when it's raised in reset_password.py?
Oh yeah, I forgot about that promise ;)
Hopefully, I can start the frontend series soon. Before that, I want to make some improvements to this backend like adding images.
About
ExpiredTokenError
that's a mistake, I should add ExpiredTokenError in errors.py as well.Hi Paurakh,
How to import in case of 2 different error modules, for example
resource_errors and user_errors?
I tried as below but its not working
from resources.errors import resource_errors
from user.errors import user_errors
api = Api(app, errors={'user_errors':user_errors, 'resource_errors':resource_errors})
Will be great if you can suggest me what i am doing wrong?
I Found a solution,
the parameter error accepts only dictionary, so merge the different error dictionary into one and then pass that dictionary
example:
collective_errors = {**user_errors, **resource_errors}
api = Api(app, errors=collective_errors)
Hey Paurakh Sharma Humagain I've been getting this error (TypeError: init() takes 1 positional argument but 2 were given) more often on the ListField as well as the ReferenceField. Please help me out How can i go past this error?
Great implementation!
One quick note, you can avoid handling your exceptions with try-catch by extending the HTTPException class.
FlaskRESTful will handle these exceptions for you.
Secondly, you can avoid defining a 'pass' in error classes like InternalServerError, seeing the it already exists in Werkzeug.
FlaskRESTful will access your errors dictionary and display your JSON.
Lovely read once again, you improved my understanding.
Cheers.
How do I pass some custom "message" or "info" data/fields to the Errors defined in this manner?
This is one of the best tutorials I have ever folowed, the best part being that everything works. I did not have to look up why things aren't working.