fix(web): send JSON for all errors (#464)

Configure werkzeug to output JSON error messages for the benefit of
downstream clients. This also allows for metrics collection in all cases
where werkzeug is outputting an exception.
This commit is contained in:
Terin Stock 2016-10-26 00:46:43 -07:00 committed by kevgliss
parent cd9c112218
commit 4b7fc8551c

View File

@ -65,7 +65,8 @@ def configure_hook(app):
:param app: :param app:
:return: :return:
""" """
from flask.ext.principal import PermissionDenied from flask import jsonify
from werkzeug.exceptions import default_exceptions
from lemur.decorators import crossdomain from lemur.decorators import crossdomain
if app.config.get('CORS'): if app.config.get('CORS'):
@app.after_request @app.after_request
@ -73,17 +74,13 @@ def configure_hook(app):
def after(response): def after(response):
return response return response
@app.errorhandler(500) def make_json_handler(code):
def internal_error(error): def json_handler(error):
metrics.send('500_status_code', 'counter', 1) metrics.send('{}_status_code'.format(code), 'counter', 1)
response = jsonify(message=str(error))
response.status_code = code
return response
return json_handler
@app.errorhandler(400) for code in default_exceptions.iterkeys():
def response_error(error): app.error_handler_spec[None][code] = make_json_handler(code)
metrics.send('400_status_code', 'counter', 1)
@app.errorhandler(PermissionDenied)
def permission_denied_error(error):
metrics.send('403_status_code', 'counter', 1)
response = {'message': 'You are not allow to access this resource'}
response.status_code = 403
return response