Add catch_all_405s or ability to define custom error handler for 405 MethodNotAllowed Error
See original GitHub issueErrors 404 and 405 are treated in a special way by Flask-Restful (see _should_use_fr_error_handler()).
In case of 404 NotFound, we can set the catch_all_404s=False
flag, so that Flask’s error handle will deal with the 404 NotFound exception.
There is no equivalent feature for 405 MethodNotAllowed. And so every time a request hits 405, the response will be {'message': 'The method is not allowed for the requested URL.'}
. And that is even after registering custom error handler to Flask object: app.register_error_handler(Exception, my_custom_error_handler)
It would be really useful to be able to set catch_all_405s=False, or ideally be able to define our own error handler for 405 error.
Currently the only way to specify custom 405 error is by defining a non-dynamic dictionary: e.g.
'MethodNotAllowed': {
'type': 'MethodNotAllowed',
'description': 'The method is not allowed for the requested URL.',
'url': None,
'data': None,
'status': 405
}
This however won’t allow us to update the dict elements such as ‘url’ in the given example.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
2 years later and I’ve hit this problem again. For those who may need this, here’s the solution.
If you want to replace the
flask-restful
405 error message fromto your custom message generated by your own handler , e.g.
you need to use Flask’s
got_request_exception
to catch when an exception happens during request processing:application.py
:errors.py
:Thank you for this 😃