Skip to content

Commit 9caad04

Browse files
committed
Added exception handling for client side ValueError
1 parent 09a22fe commit 9caad04

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

google/cloud/bigtable/row.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def commit(self):
467467
"""
468468
try:
469469
self._table._table_impl.mutate_row(self.row_key, self._get_mutations())
470-
return status_pb2.Status()
470+
return status_pb2.Status(code=code_pb2.OK)
471471
except GoogleAPICallError as e:
472472
# If the RPC call returns an error, extract the error into a status object, if possible.
473473
return status_pb2.Status(
@@ -477,6 +477,12 @@ def commit(self):
477477
message=e.message,
478478
details=e.details,
479479
)
480+
except ValueError as e:
481+
# _table_impl.mutate_row raises a ValueError if invalid arguments are provided.
482+
return status_pb2.Status(
483+
code=code_pb2.INVALID_ARGUMENT,
484+
message=str(e),
485+
)
480486
finally:
481487
self.clear()
482488

tests/system/v2_client/test_data_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,10 @@ def test_table_direct_row_input_errors(data_table, rows_to_delete):
10921092
resp = row.commit()
10931093
assert resp.code == StatusCode.INVALID_ARGUMENT.value[0]
10941094

1095-
# Not having any mutations gives a ValueError enforced on the client side.
1095+
# Not having any mutations gives an INVALID_ARGUMENT
10961096
row.clear()
1097-
with pytest.raises(ValueError):
1098-
row.commit()
1097+
resp = row.commit()
1098+
assert resp.code == StatusCode.INVALID_ARGUMENT.value[0]
10991099

11001100

11011101
def test_table_conditional_row_input_errors(data_table, rows_to_delete):

tests/unit/v2_client/test_row.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,34 @@ def test_direct_row_commit_with_unknown_exception():
474474
assert app_profile_id == call_args.app_profile_id[0]
475475

476476

477+
def test_direct_row_commit_with_invalid_argument():
478+
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
479+
from google.rpc import code_pb2, status_pb2
480+
481+
project_id = "project-id"
482+
row_key = b"row_key"
483+
table_name = "projects/more-stuff"
484+
app_profile_id = "app_profile_id"
485+
486+
credentials = _make_credentials()
487+
client = _make_client(project=project_id, credentials=credentials, admin=True)
488+
table = _Table(table_name, client=client, app_profile_id=app_profile_id)
489+
row = _make_direct_row(row_key, table)
490+
491+
# Set mock
492+
api = mock.create_autospec(BigtableClient)
493+
client.table_data_client
494+
client._table_data_client._gapic_client = api
495+
496+
# Perform the method and check the result.
497+
result = row.commit()
498+
assert row._mutations == []
499+
assert result == status_pb2.Status(
500+
code=code_pb2.Code.INVALID_ARGUMENT, message="No mutations provided"
501+
)
502+
api.mutate_row.assert_not_called()
503+
504+
477505
def _make_conditional_row(*args, **kwargs):
478506
from google.cloud.bigtable.row import ConditionalRow
479507

0 commit comments

Comments
 (0)