mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 08:02:31 -05:00
Rest methods that explicitly return errors by pass the error handlers as
they do not raise exceptions. Introduced a simple exception that contains error messages and status code that can be used rather than return json + status code from rest methods directly. The handler in errors for this exception can then log the error before returning json.
This commit is contained in:
@@ -6,14 +6,36 @@ from sqlalchemy.exc import SQLAlchemyError, DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
|
||||
class InvalidData(Exception):
|
||||
|
||||
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 __str__(self):
|
||||
return str(self.to_dict())
|
||||
|
||||
|
||||
def register_errors(blueprint):
|
||||
|
||||
@blueprint.app_errorhandler(InvalidData)
|
||||
def invalid_data(error):
|
||||
response = jsonify(error.to_dict())
|
||||
response.status_code = error.status_code
|
||||
current_app.logger.error(error)
|
||||
return response
|
||||
|
||||
@blueprint.app_errorhandler(400)
|
||||
def bad_request(e):
|
||||
if isinstance(e, str):
|
||||
msg = e
|
||||
else:
|
||||
msg = e.description or "Invalid request parameters"
|
||||
current_app.logger.exception(msg)
|
||||
return jsonify(result='error', message=str(msg)), 400
|
||||
|
||||
@blueprint.app_errorhandler(401)
|
||||
@@ -32,17 +54,16 @@ def register_errors(blueprint):
|
||||
msg = e
|
||||
else:
|
||||
msg = e.description or "Not found"
|
||||
current_app.logger.exception(msg)
|
||||
return jsonify(result='error', message=msg), 404
|
||||
|
||||
@blueprint.app_errorhandler(429)
|
||||
def limit_exceeded(e):
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(result='error', message=str(e.description)), 429
|
||||
|
||||
@blueprint.app_errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
if isinstance(e, str):
|
||||
current_app.logger.exception(e)
|
||||
elif isinstance(e, Exception):
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(result='error', message="Internal server error"), 500
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request,
|
||||
current_app
|
||||
request
|
||||
)
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
@@ -16,7 +15,7 @@ template_statistics = Blueprint('template-statistics',
|
||||
__name__,
|
||||
url_prefix='/service/<service_id>/template-statistics')
|
||||
|
||||
from app.errors import register_errors
|
||||
from app.errors import register_errors, InvalidData
|
||||
|
||||
register_errors(template_statistics)
|
||||
|
||||
@@ -28,14 +27,14 @@ def get_template_statistics_for_service(service_id):
|
||||
limit_days = int(request.args['limit_days'])
|
||||
except ValueError as e:
|
||||
error = '{} is not an integer'.format(request.args['limit_days'])
|
||||
current_app.logger.error(error)
|
||||
return jsonify(result="error", message={'limit_days': [error]}), 400
|
||||
message = {'limit_days': [error]}
|
||||
raise InvalidData(message, status_code=400)
|
||||
else:
|
||||
limit_days = None
|
||||
stats = dao_get_template_statistics_for_service(service_id, limit_days=limit_days)
|
||||
data, errors = template_statistics_schema.dump(stats, many=True)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
raise InvalidData(errors, status_code=400)
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@@ -44,5 +43,5 @@ def get_template_statistics_for_template_id(service_id, template_id):
|
||||
stats = dao_get_template_statistics_for_template(template_id)
|
||||
data, errors = template_statistics_schema.dump(stats, many=True)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
raise InvalidData(errors, status_code=400)
|
||||
return jsonify(data=data)
|
||||
|
||||
Reference in New Issue
Block a user