Reduce logging level

If the organisation name that is being inserted or updated is not unique we just want to return a 400 to the admin app.
Updated the code so that we are not logging.exception, this is because a cloud watch alert is set to the support team. This type of error is not something we need to investigate.
This commit is contained in:
Rebecca Law
2018-02-14 14:35:40 +00:00
parent 15c2a9461e
commit 6f488bf7c7
2 changed files with 20 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
from flask import Blueprint, current_app, jsonify, request from flask import Blueprint, jsonify, request
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from app.dao.organisation_dao import ( from app.dao.organisation_dao import (
@@ -29,11 +29,7 @@ def handle_integrity_error(exc):
Handle integrity errors caused by the unique contraint on ix_organisation_name Handle integrity errors caused by the unique contraint on ix_organisation_name
""" """
if 'ix_organisation_name' in str(exc): if 'ix_organisation_name' in str(exc):
current_app.logger.exception('Unique constraint ix_organisation_name triggered') raise InvalidRequest('Organisation name already exists', 400)
return jsonify(
result='error',
message='Organisation name already exists'
), 400
raise raise

View File

@@ -1,3 +1,6 @@
import pytest
from app.errors import InvalidRequest
from app.models import Organisation from app.models import Organisation
from app.dao.organisation_dao import dao_add_service_to_organisation from app.dao.organisation_dao import dao_add_service_to_organisation
from tests.app.db import create_organisation, create_service from tests.app.db import create_organisation, create_service
@@ -59,16 +62,17 @@ def test_post_create_organisation_existing_name_raises_400(admin_request, sample
'active': True 'active': True
} }
response = admin_request.post( with pytest.raises(InvalidRequest) as e:
'organisation.create_organisation', admin_request.post(
_data=data, 'organisation.create_organisation',
_expected_status=400 _data=data,
) _expected_status=400
)
organisation = Organisation.query.all() organisation = Organisation.query.all()
assert len(organisation) == 1 assert len(organisation) == 1
assert response['message'] == 'Organisation name already exists' assert 'Organisation name already exists' in str(e.value)
def test_post_create_organisation_with_missing_name_gives_validation_error(admin_request, notify_db_session): def test_post_create_organisation_with_missing_name_gives_validation_error(admin_request, notify_db_session):
@@ -117,14 +121,15 @@ def test_post_update_organisation_raises_400_on_existing_org_name(
'active': False 'active': False
} }
response = admin_request.post( with pytest.raises(expected_exception=InvalidRequest) as e:
'organisation.update_organisation', admin_request.post(
_data=data, 'organisation.update_organisation',
organisation_id=org.id, _data=data,
_expected_status=400 organisation_id=org.id,
) _expected_status=400
)
assert response['message'] == 'Organisation name already exists' assert 'Organisation name already exists' in str(e.value)
def test_post_update_organisation_gives_404_status_if_org_does_not_exist(admin_request, notify_db_session): def test_post_update_organisation_gives_404_status_if_org_does_not_exist(admin_request, notify_db_session):