2016-02-17 17:04:50 +00:00
|
|
|
from flask import (
|
|
|
|
|
jsonify,
|
|
|
|
|
current_app
|
|
|
|
|
)
|
2016-02-19 11:37:35 +00:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError, DataError
|
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2015-12-11 16:57:00 +00:00
|
|
|
|
|
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
def register_errors(blueprint):
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
@blueprint.app_errorhandler(400)
|
|
|
|
|
def bad_request(e):
|
2016-02-26 12:00:16 +00:00
|
|
|
if isinstance(e, str):
|
|
|
|
|
msg = e
|
|
|
|
|
else:
|
|
|
|
|
msg = e.description or "Invalid request parameters"
|
|
|
|
|
return jsonify(result='error', message=str(msg)), 400
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
@blueprint.app_errorhandler(401)
|
|
|
|
|
def unauthorized(e):
|
|
|
|
|
error_message = "Unauthorized, authentication token must be provided"
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message=error_message), 401, [('WWW-Authenticate', 'Bearer')]
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
@blueprint.app_errorhandler(403)
|
|
|
|
|
def forbidden(e):
|
|
|
|
|
error_message = "Forbidden, invalid authentication token provided"
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message=error_message), 403
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
@blueprint.app_errorhandler(404)
|
|
|
|
|
def page_not_found(e):
|
2016-02-26 12:00:16 +00:00
|
|
|
if isinstance(e, str):
|
|
|
|
|
msg = e
|
|
|
|
|
else:
|
|
|
|
|
msg = e.description or "Not found"
|
|
|
|
|
return jsonify(result='error', message=msg), 404
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
@blueprint.app_errorhandler(429)
|
|
|
|
|
def limit_exceeded(e):
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message=str(e.description)), 429
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
@blueprint.app_errorhandler(500)
|
|
|
|
|
def internal_server_error(e):
|
2016-02-26 12:00:16 +00:00
|
|
|
if isinstance(e, str):
|
2016-03-08 15:47:35 +00:00
|
|
|
current_app.logger.exception(e)
|
2016-02-26 12:00:16 +00:00
|
|
|
elif isinstance(e, Exception):
|
|
|
|
|
current_app.logger.exception(e)
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message="Internal server error"), 500
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
@blueprint.app_errorhandler(NoResultFound)
|
|
|
|
|
def no_result_found(e):
|
|
|
|
|
current_app.logger.error(e)
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message="No result found"), 404
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
@blueprint.app_errorhandler(DataError)
|
2016-02-25 15:35:31 +00:00
|
|
|
def data_error(e):
|
2016-02-19 11:37:35 +00:00
|
|
|
current_app.logger.error(e)
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message="No result found"), 404
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
@blueprint.app_errorhandler(SQLAlchemyError)
|
|
|
|
|
def db_error(e):
|
|
|
|
|
current_app.logger.error(e)
|
2016-02-25 12:11:51 +00:00
|
|
|
return jsonify(result='error', message=str(e)), 500
|