diff --git a/.gitignore b/.gitignore index 18542dd47..e4f8ff58e 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,5 @@ celerybeat-schedule # CloudFoundry .cf + +/scripts/run_my_tests.sh diff --git a/app/errors.py b/app/errors.py index 1a2d3e479..c2461f3cd 100644 --- a/app/errors.py +++ b/app/errors.py @@ -3,7 +3,7 @@ from flask import ( current_app, json) from notifications_utils.recipients import InvalidEmailError -from sqlalchemy.exc import SQLAlchemyError, DataError +from sqlalchemy.exc import DataError from sqlalchemy.orm.exc import NoResultFound from marshmallow import ValidationError from jsonschema import ValidationError as JsonSchemaValidationError @@ -95,20 +95,6 @@ def register_errors(blueprint): current_app.logger.info(e) return jsonify(result='error', message="No result found"), 404 - @blueprint.errorhandler(SQLAlchemyError) - def db_error(e): - if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and 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 - current_app.logger.exception(e) - 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) diff --git a/app/organisation/rest.py b/app/organisation/rest.py index 68bbe9119..6e29c9e89 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -1,4 +1,4 @@ -from flask import Blueprint, current_app, jsonify, request +from flask import Blueprint, jsonify, request, current_app from sqlalchemy.exc import IntegrityError from app.dao.organisation_dao import ( @@ -26,16 +26,14 @@ register_errors(organisation_blueprint) @organisation_blueprint.errorhandler(IntegrityError) def handle_integrity_error(exc): """ - Handle integrity errors caused by the unique contraint on ix_organisation_name + Handle integrity errors caused by the unique constraint on ix_organisation_name """ if 'ix_organisation_name' in str(exc): - current_app.logger.exception('Unique constraint ix_organisation_name triggered') - return jsonify( - result='error', - message='Organisation name already exists' - ), 400 + return jsonify(result="error", + message="Organisation name already exists"), 400 - raise + current_app.logger.exception(exc) + return jsonify(result='error', message="Internal server error"), 500 @organisation_blueprint.route('', methods=['GET']) @@ -61,7 +59,6 @@ def create_organisation(): organisation = Organisation(**data) dao_create_organisation(organisation) - return jsonify(organisation.serialize()), 201 @@ -70,7 +67,6 @@ def update_organisation(organisation_id): data = request.get_json() validate(data, post_update_organisation_schema) result = dao_update_organisation(organisation_id, **data) - if result: return '', 204 else: diff --git a/app/service/rest.py b/app/service/rest.py index 8d74124a2..99f03cee9 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -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' diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 0bcd56381..676d72a22 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -110,7 +110,7 @@ def test_post_update_organisation_updates_fields(admin_request, notify_db_sessio def test_post_update_organisation_raises_400_on_existing_org_name( - admin_request, notify_db_session, sample_organisation): + admin_request, sample_organisation): org = create_organisation() data = { 'name': sample_organisation.name,