Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Ignore pytest cache directory
  Add error handling for invalid responses
  • Loading branch information
ckoehn committed Apr 27, 2018
2 parents 172b514 + 3f5151e commit 26831cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ChangeLog

# pytest files
/.cache
/.pytest_cache

# Build and artifact dirs
/build
Expand Down
9 changes: 8 additions & 1 deletion festung/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,16 @@ def _request(self, *args, **kwargs):
return self.connection._request(*args, **kwargs)
except requests.HTTPError as e:
self._raise_for_error(e.response)
except requests.RequestException as e:
raise InternalError(e)

def _raise_for_error(self, response):
error = response.json()['error']
try:
error = response.json()['error']
except ValueError as e:
raise InternalError("Invalid JSON: {}".format(e))
except KeyError as e:
raise InternalError("Missing error in response.")
type_ = error['type']
description = error['description']
exception_class = error_to_exception[type_]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ def run_query(self, cursor, exc_type):
return excinfo.value


class TestInvalidDataResponse(object):
@pytest.mark.parametrize('data,status,exc_class', [
('{', httplib.INTERNAL_SERVER_ERROR, exceptions.InternalError),
('{}', httplib.INTERNAL_SERVER_ERROR, exceptions.InternalError),
])
def test_execute(self, festung, cursor, data, status, exc_class):
festung.add_response(data, status)
with pytest.raises(exc_class):
cursor.execute('SELECT 1;')


class TestCustomMethods(object):
def test_drop(self, festung, cursor, database, password):
festung.add_empty_response()
Expand Down

0 comments on commit 26831cb

Please sign in to comment.