Skip to content

Commit

Permalink
use api_core exception
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelnucfin committed Dec 20, 2017
1 parent e17ef82 commit 59c6a23
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions spanner/google/cloud/spanner_v1/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions spanner/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 59c6a23

Please sign in to comment.