mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-05 08:40:29 -04: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:
@@ -1,4 +1,4 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
from flask import Blueprint, jsonify, request, current_app
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.dao.organisation_dao import (
|
||||
@@ -26,12 +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):
|
||||
raise InvalidRequest('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'])
|
||||
@@ -57,7 +59,6 @@ def create_organisation():
|
||||
|
||||
organisation = Organisation(**data)
|
||||
dao_create_organisation(organisation)
|
||||
|
||||
return jsonify(organisation.serialize()), 201
|
||||
|
||||
|
||||
@@ -66,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:
|
||||
|
||||
Reference in New Issue
Block a user