Wire up error handlers.

Replace some 400s with more appropriate 500s.

DAO methods that cause unexpected exceptions get caught and
logged by errors.py 500 error handler.
This commit is contained in:
Adam Shimali
2016-02-17 17:04:50 +00:00
parent b98d8b9996
commit 4f33b6f406
7 changed files with 66 additions and 55 deletions

View File

@@ -1,36 +1,34 @@
from flask import (jsonify, current_app)
from app.main import main
from flask import (
jsonify,
current_app
)
@main.app_errorhandler(400)
def bad_request(e):
return jsonify(error=str(e.description)), 400
def register_errors(blueprint):
@blueprint.app_errorhandler(400)
def bad_request(e):
return jsonify(error=str(e.description)), 400
@main.app_errorhandler(401)
def unauthorized(e):
error_message = "Unauthorized, authentication token must be provided"
return jsonify(error=error_message), 401, [('WWW-Authenticate', 'Bearer')]
@blueprint.app_errorhandler(401)
def unauthorized(e):
error_message = "Unauthorized, authentication token must be provided"
return jsonify(error=error_message), 401, [('WWW-Authenticate', 'Bearer')]
@blueprint.app_errorhandler(403)
def forbidden(e):
error_message = "Forbidden, invalid authentication token provided"
return jsonify(error=error_message), 403
@main.app_errorhandler(403)
def forbidden(e):
error_message = "Forbidden, invalid authentication token provided"
return jsonify(error=error_message), 403
@blueprint.app_errorhandler(404)
def page_not_found(e):
return jsonify(error=e.description or "Not found"), 404
@blueprint.app_errorhandler(429)
def limit_exceeded(e):
return jsonify(error=str(e.description)), 429
@main.app_errorhandler(404)
def page_not_found(e):
return jsonify(error=e.description or "Not found"), 404
@main.app_errorhandler(429)
def limit_exceeded(e):
return jsonify(error=str(e.description)), 429
@main.app_errorhandler(500)
def internal_server_error(e):
current_app.logger.exception(e)
return jsonify(error="Internal error"), 500
@blueprint.app_errorhandler(500)
def internal_server_error(e):
current_app.logger.exception(e)
return jsonify(error="Internal error"), 500