mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-12 16:22:17 -05:00
There is a uniqueness constraint on service `name` and `email_from`. When you try to insert a row which has _both_ constraints, there is no guarantee which one the operation will fail on. This means that, when handling the exception, service `name` is not reliably available, because sometimes the operation fails on the `email_from` constraint instead. This caused the tests to fail non-deterministically because they were looking for the service `name` in the error message. As a fix, this commit does two things: 1. Return either the service `name` or `email_from` in the error message, depending which is available. 2. Modify the test to pass on _either_ of the two possible error messages. This is not ideal, but I can’t think of a way to maintain the original behaviour, and have reliably passing tests.
72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
from flask import (
|
|
jsonify,
|
|
current_app
|
|
)
|
|
from sqlalchemy.exc import SQLAlchemyError, DataError
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
|
def register_errors(blueprint):
|
|
|
|
@blueprint.app_errorhandler(400)
|
|
def bad_request(e):
|
|
if isinstance(e, str):
|
|
msg = e
|
|
else:
|
|
msg = e.description or "Invalid request parameters"
|
|
return jsonify(result='error', message=str(msg)), 400
|
|
|
|
@blueprint.app_errorhandler(401)
|
|
def unauthorized(e):
|
|
error_message = "Unauthorized, authentication token must be provided"
|
|
return jsonify(result='error', message=error_message), 401, [('WWW-Authenticate', 'Bearer')]
|
|
|
|
@blueprint.app_errorhandler(403)
|
|
def forbidden(e):
|
|
error_message = "Forbidden, invalid authentication token provided"
|
|
return jsonify(result='error', message=error_message), 403
|
|
|
|
@blueprint.app_errorhandler(404)
|
|
def page_not_found(e):
|
|
if isinstance(e, str):
|
|
msg = e
|
|
else:
|
|
msg = e.description or "Not found"
|
|
return jsonify(result='error', message=msg), 404
|
|
|
|
@blueprint.app_errorhandler(429)
|
|
def limit_exceeded(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
|
|
|
|
@blueprint.app_errorhandler(NoResultFound)
|
|
def no_result_found(e):
|
|
current_app.logger.exception(e)
|
|
return jsonify(result='error', message="No result found"), 404
|
|
|
|
@blueprint.app_errorhandler(DataError)
|
|
def data_error(e):
|
|
current_app.logger.exception(e)
|
|
return jsonify(result='error', message="No result found"), 404
|
|
|
|
@blueprint.app_errorhandler(SQLAlchemyError)
|
|
def db_error(e):
|
|
current_app.logger.exception(e)
|
|
if e.orig.pgerror and \
|
|
('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',
|
|
message={'name': ["Duplicate service name '{}'".format(
|
|
e.params.get('name', e.params.get('email_from', ''))
|
|
)]}
|
|
), 400
|
|
return jsonify(result='error', message="Internal server error"), 500
|