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:
parent
cd9c112218
commit
4b7fc8551c
@ -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))
|
||||||
@app.errorhandler(400)
|
response.status_code = code
|
||||||
def response_error(error):
|
|
||||||
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
|
return response
|
||||||
|
return json_handler
|
||||||
|
|
||||||
|
for code in default_exceptions.iterkeys():
|
||||||
|
app.error_handler_spec[None][code] = make_json_handler(code)
|
||||||
|
Loading…
Reference in New Issue
Block a user