2016-02-17 17:04:50 +00:00
|
|
|
from flask import (
|
|
|
|
|
jsonify,
|
2017-06-09 10:34:02 +01:00
|
|
|
current_app,
|
|
|
|
|
json)
|
2018-01-19 12:23:07 +00:00
|
|
|
from notifications_utils.recipients import InvalidEmailError
|
2018-02-19 14:33:44 +00:00
|
|
|
from sqlalchemy.exc import DataError
|
2016-02-19 11:37:35 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-06-14 15:07:23 +01:00
|
|
|
from marshmallow import ValidationError
|
2017-06-09 10:34:02 +01:00
|
|
|
from jsonschema import ValidationError as JsonSchemaValidationError
|
2016-06-29 17:37:20 +01:00
|
|
|
from app.authentication.auth import AuthError
|
2018-04-25 16:34:36 +01:00
|
|
|
from app.exceptions import ArchiveValidationError
|
2015-12-11 16:57:00 +00:00
|
|
|
|
|
|
|
|
|
2018-04-03 12:31:52 +01:00
|
|
|
class VirusScanError(Exception):
|
|
|
|
|
def __init__(self, message):
|
|
|
|
|
|
|
|
|
|
super().__init__(message)
|
|
|
|
|
|
|
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
class InvalidRequest(Exception):
|
2016-10-27 11:46:37 +01:00
|
|
|
code = None
|
|
|
|
|
fields = []
|
2016-06-13 11:48:20 +01:00
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
]
|
2016-10-27 11:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-13 11:48:20 +01:00
|
|
|
def __str__(self):
|
|
|
|
|
return str(self.to_dict())
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
def register_errors(blueprint):
|
2018-01-19 12:23:07 +00:00
|
|
|
@blueprint.errorhandler(InvalidEmailError)
|
|
|
|
|
def invalid_format(error):
|
|
|
|
|
# Please not that InvalidEmailError is re-raised for InvalidEmail or InvalidPhone,
|
|
|
|
|
# work should be done in the utils app to tidy up these errors.
|
|
|
|
|
return jsonify(result='error', message=str(error)), 400
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(AuthError)
|
2016-06-29 17:37:20 +01:00
|
|
|
def authentication_error(error):
|
|
|
|
|
return jsonify(result='error', message=error.message), error.code
|
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(ValidationError)
|
2017-11-16 17:46:41 +00:00
|
|
|
def marshmallow_validation_error(error):
|
2018-02-08 13:38:32 +00:00
|
|
|
current_app.logger.info(error)
|
2016-06-14 15:07:23 +01:00
|
|
|
return jsonify(result='error', message=error.messages), 400
|
|
|
|
|
|
2017-06-09 10:34:02 +01:00
|
|
|
@blueprint.errorhandler(JsonSchemaValidationError)
|
2017-11-16 17:46:41 +00:00
|
|
|
def jsonschema_validation_error(error):
|
2018-02-08 13:38:32 +00:00
|
|
|
current_app.logger.info(error)
|
2017-06-09 10:34:02 +01:00
|
|
|
return jsonify(json.loads(error.message)), 400
|
|
|
|
|
|
2018-04-25 16:34:36 +01:00
|
|
|
@blueprint.errorhandler(ArchiveValidationError)
|
|
|
|
|
def archive_validation_error(error):
|
|
|
|
|
current_app.logger.info(error)
|
|
|
|
|
return jsonify(result='error', message=str(error)), 400
|
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(InvalidRequest)
|
2016-06-13 11:48:20 +01:00
|
|
|
def invalid_data(error):
|
|
|
|
|
response = jsonify(error.to_dict())
|
|
|
|
|
response.status_code = error.status_code
|
2018-02-08 13:38:32 +00:00
|
|
|
current_app.logger.info(error)
|
2016-06-13 11:48:20 +01:00
|
|
|
return response
|
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(400)
|
2016-02-17 17:04:50 +00:00
|
|
|
def bad_request(e):
|
2016-10-17 17:41:39 +01:00
|
|
|
msg = e.description or "Invalid request parameters"
|
2016-06-13 11:48:20 +01:00
|
|
|
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
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(401)
|
2016-02-17 17:04:50 +00:00
|
|
|
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-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(403)
|
2016-02-17 17:04:50 +00:00
|
|
|
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-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(429)
|
2016-02-17 17:04:50 +00:00
|
|
|
def limit_exceeded(e):
|
2016-06-13 11:48:20 +01:00
|
|
|
current_app.logger.exception(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-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(NoResultFound)
|
|
|
|
|
@blueprint.errorhandler(DataError)
|
2016-10-17 17:41:39 +01:00
|
|
|
def no_result_found(e):
|
2018-02-08 13:38:32 +00:00
|
|
|
current_app.logger.info(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
|
|
|
|
2016-10-17 17:41:39 +01:00
|
|
|
# this must be defined after all other error handlers since it catches the generic Exception object
|
|
|
|
|
@blueprint.app_errorhandler(500)
|
2016-10-27 11:46:37 +01:00
|
|
|
@blueprint.errorhandler(Exception)
|
2016-10-17 17:41:39 +01:00
|
|
|
def internal_server_error(e):
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
return jsonify(result='error', message="Internal server error"), 500
|