mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Catch itegrity errors and return 400.
When creating or updating an organisation an itegrity error is raise if the name is already used. This change adds a new error handler for the organisation to catch the named unique index and return a 400 with a sensible message. We have an other error handler for unique service names which was caught in the error handler for all blueprints. A new error handler for the service_blueprint has been created for catch those specific unique constraints. This is a nice way to encapulate the specific errors for a specific blueprint.
This commit is contained in:
@@ -7,6 +7,7 @@ from flask import (
|
||||
current_app,
|
||||
Blueprint
|
||||
)
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app.dao import notifications_dao
|
||||
@@ -94,6 +95,22 @@ service_blueprint = Blueprint('service', __name__)
|
||||
register_errors(service_blueprint)
|
||||
|
||||
|
||||
@service_blueprint.errorhandler(IntegrityError)
|
||||
def handle_integrity_error(exc):
|
||||
"""
|
||||
Handle integrity errors caused by the unique constraint on ix_organisation_name
|
||||
"""
|
||||
if 'services_name_key' or 'services_email_from_key' in str(exc):
|
||||
return jsonify(
|
||||
result='error',
|
||||
message={'name': ["Duplicate service name '{}'".format(
|
||||
exc.params.get('name', exc.params.get('email_from', ''))
|
||||
)]}
|
||||
), 400
|
||||
current_app.logger.exception(exc)
|
||||
return jsonify(result='error', message="Internal server error"), 500
|
||||
|
||||
|
||||
@service_blueprint.route('/platform-stats', methods=['GET'])
|
||||
def get_platform_stats():
|
||||
include_from_test_key = request.args.get('include_from_test_key', 'True') != 'False'
|
||||
|
||||
Reference in New Issue
Block a user