Reraise integrity errors on org name unique contraints as 400

- currently being raised as 500s, but should really be 400s as the request data has the duplicate name
This commit is contained in:
Ken Tsang
2018-02-13 14:47:03 +00:00
parent d80c7d4f65
commit 4576bdd64f
2 changed files with 53 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
from flask import Blueprint, jsonify, request
from flask import Blueprint, current_app, jsonify, request
from sqlalchemy.exc import IntegrityError
from app.dao.organisation_dao import (
dao_create_organisation,
@@ -22,6 +23,21 @@ organisation_blueprint = Blueprint('organisation', __name__)
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
"""
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
raise
@organisation_blueprint.route('', methods=['GET'])
def get_organisations():
organisations = [