Skip to content

Commit dffcf13

Browse files
authored
fix: handle google.api_core.exceptions.OutOfRange exception and throw InegrityError as expected by dbapi standards (#571)
1 parent 1a0a435 commit dffcf13

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

google/cloud/spanner_dbapi/cursor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.api_core.exceptions import FailedPrecondition
2222
from google.api_core.exceptions import InternalServerError
2323
from google.api_core.exceptions import InvalidArgument
24+
from google.api_core.exceptions import OutOfRange
2425

2526
from collections import namedtuple
2627

@@ -241,7 +242,7 @@ def execute(self, sql, args=None):
241242
self.connection.database.run_in_transaction(
242243
self._do_execute_update, sql, args or None
243244
)
244-
except (AlreadyExists, FailedPrecondition) as e:
245+
except (AlreadyExists, FailedPrecondition, OutOfRange) as e:
245246
raise IntegrityError(e.details if hasattr(e, "details") else e)
246247
except InvalidArgument as e:
247248
raise ProgrammingError(e.details if hasattr(e, "details") else e)

tests/unit/spanner_dbapi/test_cursor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ def test_execute_integrity_error(self):
251251
with self.assertRaises(IntegrityError):
252252
cursor.execute(sql="sql")
253253

254+
with mock.patch(
255+
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
256+
side_effect=exceptions.OutOfRange("message"),
257+
):
258+
with self.assertRaises(IntegrityError):
259+
cursor.execute("sql")
260+
254261
def test_execute_invalid_argument(self):
255262
from google.api_core import exceptions
256263
from google.cloud.spanner_dbapi.exceptions import ProgrammingError

0 commit comments

Comments
 (0)