Files
notifications-api/app/errors.py
T

112 lines
3.8 KiB
Python
Raw Normal View History

2016-02-17 17:04:50 +00:00
from flask import (
jsonify,
current_app,
json)
from sqlalchemy.exc import SQLAlchemyError, DataError
from sqlalchemy.orm.exc import NoResultFound
from marshmallow import ValidationError
from jsonschema import ValidationError as JsonSchemaValidationError
from app.authentication.auth import AuthError
2015-12-11 16:57:00 +00:00
class InvalidRequest(Exception):
code = None
fields = []
def __init__(self, message, status_code):
super().__init__()
self.message = message
self.status_code = status_code
def to_dict(self):
return {'result': 'error', 'message': self.message}
def to_dict_v2(self):
'''
Version 2 of the public api error response.
'''
return {
2016-11-09 14:56:54 +00:00
"status_code": self.status_code,
"errors": [
{
"error": self.__class__.__name__,
"message": self.message
}
]
}
def __str__(self):
return str(self.to_dict())
2016-02-17 17:04:50 +00:00
def register_errors(blueprint):
2015-12-11 16:57:00 +00:00
@blueprint.errorhandler(AuthError)
def authentication_error(error):
return jsonify(result='error', message=error.message), error.code
@blueprint.errorhandler(ValidationError)
def validation_error(error):
current_app.logger.error(error)
return jsonify(result='error', message=error.messages), 400
@blueprint.errorhandler(JsonSchemaValidationError)
def validation_error(error):
current_app.logger.exception(error)
return jsonify(json.loads(error.message)), 400
@blueprint.errorhandler(InvalidRequest)
def invalid_data(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
current_app.logger.error(error)
return response
@blueprint.errorhandler(400)
2016-02-17 17:04:50 +00:00
def bad_request(e):
msg = e.description or "Invalid request parameters"
current_app.logger.exception(msg)
2016-02-26 12:00:16 +00:00
return jsonify(result='error', message=str(msg)), 400
2015-12-11 16:57:00 +00:00
@blueprint.errorhandler(401)
2016-02-17 17:04:50 +00:00
def unauthorized(e):
error_message = "Unauthorized, authentication token must be provided"
return jsonify(result='error', message=error_message), 401, [('WWW-Authenticate', 'Bearer')]
2015-12-11 16:57:00 +00:00
@blueprint.errorhandler(403)
2016-02-17 17:04:50 +00:00
def forbidden(e):
error_message = "Forbidden, invalid authentication token provided"
return jsonify(result='error', message=error_message), 403
2015-12-11 16:57:00 +00:00
@blueprint.errorhandler(429)
2016-02-17 17:04:50 +00:00
def limit_exceeded(e):
current_app.logger.exception(e)
return jsonify(result='error', message=str(e.description)), 429
2015-12-11 16:57:00 +00:00
@blueprint.errorhandler(NoResultFound)
@blueprint.errorhandler(DataError)
def no_result_found(e):
2016-03-11 15:52:28 +00:00
current_app.logger.exception(e)
return jsonify(result='error', message="No result found"), 404
@blueprint.errorhandler(SQLAlchemyError)
def db_error(e):
2016-03-11 15:52:28 +00:00
current_app.logger.exception(e)
if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror and \
2016-04-25 10:38:37 +01:00
('duplicate key value violates unique constraint "services_name_key"' in e.orig.pgerror or
'duplicate key value violates unique constraint "services_email_from_key"' in e.orig.pgerror):
return jsonify(
result='error',
2016-04-26 09:32:27 +01:00
message={'name': ["Duplicate service name '{}'".format(
e.params.get('name', e.params.get('email_from', ''))
)]}
2016-04-25 10:38:37 +01:00
), 400
return jsonify(result='error', message="Internal server error"), 500
# this must be defined after all other error handlers since it catches the generic Exception object
@blueprint.app_errorhandler(500)
@blueprint.errorhandler(Exception)
def internal_server_error(e):
current_app.logger.exception(e)
return jsonify(result='error', message="Internal server error"), 500