Skip to content

Commit

Permalink
fix: handle google.api_core.exceptions.OutOfRange exception and throw…
Browse files Browse the repository at this point in the history
… InegrityError as expected by dbapi standards (#571)
  • Loading branch information
vi3k6i5 authored Sep 14, 2021
1 parent 1a0a435 commit dffcf13
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion google/cloud/spanner_dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from google.api_core.exceptions import FailedPrecondition
from google.api_core.exceptions import InternalServerError
from google.api_core.exceptions import InvalidArgument
from google.api_core.exceptions import OutOfRange

from collections import namedtuple

Expand Down Expand Up @@ -241,7 +242,7 @@ def execute(self, sql, args=None):
self.connection.database.run_in_transaction(
self._do_execute_update, sql, args or None
)
except (AlreadyExists, FailedPrecondition) as e:
except (AlreadyExists, FailedPrecondition, OutOfRange) as e:
raise IntegrityError(e.details if hasattr(e, "details") else e)
except InvalidArgument as e:
raise ProgrammingError(e.details if hasattr(e, "details") else e)
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/spanner_dbapi/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def test_execute_integrity_error(self):
with self.assertRaises(IntegrityError):
cursor.execute(sql="sql")

with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
side_effect=exceptions.OutOfRange("message"),
):
with self.assertRaises(IntegrityError):
cursor.execute("sql")

def test_execute_invalid_argument(self):
from google.api_core import exceptions
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
Expand Down

0 comments on commit dffcf13

Please sign in to comment.