Skip to content

Commit 3a2767d

Browse files
committed
use api_core exception
1 parent e17ef82 commit 3a2767d

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

spanner/google/cloud/spanner_v1/database.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import threading
1919

2020
import google.auth.credentials
21+
from google.api_core import exceptions
2122
from google.gax.errors import GaxError
2223
from google.gax.grpc import exc_to_code
2324
from google.cloud.spanner_v1.gapic.spanner_client import SpannerClient
@@ -208,14 +209,12 @@ def create(self):
208209
options=options,
209210
)
210211
except GaxError as exc:
211-
if exc_to_code(exc.cause) == StatusCode.ALREADY_EXISTS:
212+
exception = exceptions.from_grpc_error(exc.cause)
213+
if exception.grpc_status_code == StatusCode.ALREADY_EXISTS:
212214
raise Conflict(self.name)
213-
elif exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
214-
raise NotFound('Database not found: {name}'.format(
215-
name=db_name,
216-
))
215+
elif exception.grpc_status_code == StatusCode.NOT_FOUND:
216+
raise exception.errors[0]
217217
raise
218-
219218
return future
220219

221220
def exists(self):

spanner/tests/system/test_system.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
from google.cloud._helpers import UTC
3838
from google.cloud.exceptions import GrpcRendezvous
39-
from google.cloud.exceptions import NotFound
4039
from google.cloud.spanner_v1._helpers import TimestampWithNanoseconds
4140
from google.cloud.spanner import Client
4241
from google.cloud.spanner import KeyRange
@@ -283,17 +282,18 @@ def test_create_database(self):
283282
for database in Config.INSTANCE.list_databases()]
284283
self.assertIn(temp_db_id, database_ids)
285284

286-
def test_create_database_not_found(self):
287-
temp_db_id = 'temp_db' + unique_resource_id('_')
285+
def test_table_not_found(self):
286+
table = 'yTable'
288287
temp_db = Config.INSTANCE.database(temp_db_id, ddl_statements=[
289288
'CREATE TABLE MyTable ('
290289
' Id STRING(36) NOT NULL,'
291290
' Field1 STRING(36) NOT NULL'
292291
') PRIMARY KEY (Id)',
293-
'CREATE INDEX IDX ON yTable (Field1)'
292+
'CREATE INDEX IDX ON {table} (Field1)'.format(table)
294293
])
295-
with self.assertRaisesRegexp(NotFound, 'Database not found: ' + temp_db_id):
296-
operation = temp_db.create()
294+
with self.assertRaisesRegexp(GrpcRendezvous,
295+
'Table not found: {table}'.format(table)):
296+
operation = temp_db.create()
297297

298298
def test_update_database_ddl(self):
299299
pool = BurstyPool()

0 commit comments

Comments
 (0)