Skip to content

Commit

Permalink
add handling for integrity violations
Browse files Browse the repository at this point in the history
  • Loading branch information
BartSaelen committed Dec 18, 2014
1 parent 682df5d commit c38ec1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 11 additions & 0 deletions atramhasis/views/exception_views.py
Expand Up @@ -6,6 +6,7 @@
import logging

from pyramid.view import view_config, notfound_view_config
from sqlalchemy.exc import IntegrityError

from atramhasis.errors import SkosRegistryNotFoundException, ValidationError
from atramhasis.protected_resources import ProtectedResourceException
Expand Down Expand Up @@ -67,6 +68,16 @@ def provider_unavailable(exc, request):
return {'message': exc.message}


@view_config(context=IntegrityError, renderer='json')
def data_integrity(exc, request):
'''
View invoked when IntegrityError was raised.
'''
log.error(exc)
request.response.status_int = 409
return {'message': 'this operation violates the data integrity and could not be executed '}


@view_config(context=Exception, renderer='json')
def failed(exc, request):
'''
Expand Down
16 changes: 14 additions & 2 deletions tests/test_errors.py
@@ -1,7 +1,12 @@
import unittest

from sqlalchemy.exc import IntegrityError
try:
from unittest.mock import Mock, MagicMock
except ImportError:
from mock import Mock, MagicMock # pragma: no cover
from atramhasis.errors import SkosRegistryNotFoundException, ConceptSchemeNotFoundException, \
ConceptNotFoundException, DbNotFoundException, ValidationError, LanguageNotFoundException
from atramhasis.views.exception_views import data_integrity


class TestErrors(unittest.TestCase):
Expand Down Expand Up @@ -33,4 +38,11 @@ def test_db_error(self):
def test_language_notfound_exception(self):
error = LanguageNotFoundException("af")
self.assertIsNotNone(error)
self.assertEqual("'No language found with the given id af'", str(error))
self.assertEqual("'No language found with the given id af'", str(error))


class TestErrorsViews(unittest.TestCase):
def test_integrity(self):
error = IntegrityError(orig=MagicMock(), statement='', params={})
res = data_integrity(error, MagicMock())
self.assertEqual({'message': 'this operation violates the data integrity and could not be executed '}, res)

0 comments on commit c38ec1e

Please sign in to comment.