flask-restx
flask-restx copied to clipboard
Allow Error Handler to Return HttpResponse
I would like to have full control of the response from error handlers assigned using @api.errorhandler decorator so I can obfuscate the return of calls for some usecases.
For example:
@app.errorhandler(werkzeug.exceptions.BadRequest)
def handle_bad_request(e):
log.error("<error_dump>", error)
response = flask.make_response()
response.set_data(":(")
response.status_code = 418
response.headers['Content-Type'] = 'text/html'
response.headers['Server'] = 'Timex Sinclair'
return response
However, the code assumes full control of the response assuming that a dict is returned.
The change is quite simple. In Api.py after result = handler(e):
if not result is None and issubclass(result.__class__, BaseResponse):
return result
Created PR https://github.com/python-restx/flask-restx/pull/459
The only way to do this is at present is by doing the following which is not ideal at all and it does not allow overriding of Content-Type.
app = Flask()
app.config['ERROR_INCLUDE_MESSAGE'] = False
# ...
@app.errorhandler(werkzeug.exceptions.BadRequest)
def handle_bad_request(e):
log.error("<error_dump>", error)
return (":(", 418, Headers({'Server': 'Timex Sinclair'}))