diff --git a/spanner/google/cloud/spanner_v1/database.py b/spanner/google/cloud/spanner_v1/database.py index 6f85d30bcbd43..da6e046c7083b 100644 --- a/spanner/google/cloud/spanner_v1/database.py +++ b/spanner/google/cloud/spanner_v1/database.py @@ -17,7 +17,9 @@ import re import threading + import google.auth.credentials +from google.api_core import exceptions from google.gax.errors import GaxError from google.gax.grpc import exc_to_code from google.cloud.spanner_v1.gapic.spanner_client import SpannerClient @@ -208,14 +210,12 @@ def create(self): options=options, ) except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.ALREADY_EXISTS: + exception = exceptions.from_grpc_error(exc.cause) + if exception.grpc_status_code == StatusCode.ALREADY_EXISTS: raise Conflict(self.name) - elif exc_to_code(exc.cause) == StatusCode.NOT_FOUND: - raise NotFound('Database not found: {name}'.format( - name=db_name, - )) + elif exception.grpc_status_code == StatusCode.NOT_FOUND: + raise exception.errors[0] raise - return future def exists(self): diff --git a/spanner/tests/system/test_system.py b/spanner/tests/system/test_system.py index 5cde38d309fe1..d843bf8f51d49 100644 --- a/spanner/tests/system/test_system.py +++ b/spanner/tests/system/test_system.py @@ -36,7 +36,6 @@ from google.cloud._helpers import UTC from google.cloud.exceptions import GrpcRendezvous -from google.cloud.exceptions import NotFound from google.cloud.spanner_v1._helpers import TimestampWithNanoseconds from google.cloud.spanner import Client from google.cloud.spanner import KeyRange @@ -283,17 +282,18 @@ def test_create_database(self): for database in Config.INSTANCE.list_databases()] self.assertIn(temp_db_id, database_ids) - def test_create_database_not_found(self): - temp_db_id = 'temp_db' + unique_resource_id('_') + def test_table_not_found(self): + table = 'yTable' temp_db = Config.INSTANCE.database(temp_db_id, ddl_statements=[ 'CREATE TABLE MyTable (' ' Id STRING(36) NOT NULL,' ' Field1 STRING(36) NOT NULL' ') PRIMARY KEY (Id)', - 'CREATE INDEX IDX ON yTable (Field1)' + 'CREATE INDEX IDX ON {table} (Field1)'.format(table) ]) - with self.assertRaisesRegexp(NotFound, 'Database not found: ' + temp_db_id): - operation = temp_db.create() + with self.assertRaisesRegexp(GrpcRendezvous, + 'Table not found: {table}'.format(table)): + operation = temp_db.create() def test_update_database_ddl(self): pool = BurstyPool()