Skip to content
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

Allow Error Handler to Return HttpResponse #458

Open
JavaScriptDude opened this issue Jul 19, 2022 · 2 comments
Open

Allow Error Handler to Return HttpResponse #458

JavaScriptDude opened this issue Jul 19, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@JavaScriptDude
Copy link

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
@JavaScriptDude JavaScriptDude added the enhancement New feature or request label Jul 19, 2022
@JavaScriptDude
Copy link
Author

Created PR #459

@JavaScriptDude
Copy link
Author

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'}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant