Skip to content

Commit

Permalink
Merge pull request #146 from camptocamp/data_error
Browse files Browse the repository at this point in the history
Better handling of DataError from the DB
  • Loading branch information
Patrick Valsecchi authored May 4, 2018
2 parents 6bfb1a0 + 1d3b433 commit b769284
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion acceptance_tests/app/c2cwsgiutils_app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ def error(request):
code = int(request.params.get('code', '500'))
if code == 403:
raise HTTPForbidden('bam')
elif request.params.get('db', '0') == '1':
elif request.params.get('db', '0') == 'dup':
for _ in range(2):
models.DBSession.add(models.Hello(value='toto'))
elif request.params.get('db', '0') == 'data':
models.DBSession.add(models.Hello(id='abcd', value='toto'))
else:
raise Exception('boom')
return {'status': 200}
Expand Down
8 changes: 7 additions & 1 deletion acceptance_tests/tests/tests/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ def test_http_error(app_connection):


def test_commit_time_db(app_connection):
error = app_connection.get_json('error', params={'db': '1'}, expected_status=400)
error = app_connection.get_json('error', params={'db': 'dup'}, expected_status=400)
assert error['status'] == 400
assert 'duplicate key' in error['message']


def test_db_data_error(app_connection):
error = app_connection.get_json('error', params={'db': 'data'}, expected_status=400)
assert error['status'] == 400
assert 'invalid input syntax for integer' in error['message']


def test_other(app_connection):
error = app_connection.get_json('error', expected_status=500)
assert error['status'] == 500
Expand Down
2 changes: 2 additions & 0 deletions c2cwsgiutils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _other_error(exception: Exception, request: pyramid.request.Request) -> pyra
if exception.__class__.__module__ == 'botocore.exceptions' and \
exception.__class__.__name__ == 'ClientError':
return _boto_client_error(exception, request)
LOG.debug("Actual exception: %s.%s", exception.__class__.__module__, exception.__class__.__name__)
return _do_error(request, 500, exception)


Expand All @@ -103,6 +104,7 @@ def init(config: pyramid.config.Configurator) -> None:
common_options = {'renderer': 'json', 'http_cache': 0}
config.add_view(view=_http_error, context=HTTPException, **common_options)
config.add_view(view=_integrity_error, context=sqlalchemy.exc.IntegrityError, **common_options)
config.add_view(view=_integrity_error, context=sqlalchemy.exc.DataError, **common_options)

# We don't want to cry wolf if the user interrupted the uplad of the body
config.add_view(view=_client_interrupted_error, context=ConnectionResetError, **common_options)
Expand Down

0 comments on commit b769284

Please sign in to comment.