Skip to content

Commit 2b8d089

Browse files
authored
fix(Datastore): wrap exceptions for compatibility with V1 (#8778)
1 parent 45b3c58 commit 2b8d089

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

Datastore/src/Operation.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
namespace Google\Cloud\Datastore;
1919

20+
use Google\ApiCore\ApiException;
2021
use Google\ApiCore\Options\CallOptions;
2122
use Google\Cloud\Core\ApiHelperTrait;
2223
use Google\Cloud\Core\OptionsValidator;
24+
use Google\Cloud\Core\RequestProcessorTrait;
2325
use Google\Cloud\Core\Timestamp;
2426
use Google\Cloud\Core\TimestampTrait;
2527
use Google\Cloud\Core\ValidateTrait;
@@ -67,6 +69,7 @@ class Operation
6769
use ValidateTrait;
6870
use TimestampTrait;
6971
use ApiHelperTrait;
72+
use RequestProcessorTrait;
7073

7174
/**
7275
* @var Serializer
@@ -295,7 +298,11 @@ public function beginTransaction(array $transactionOptions, array $options = [])
295298
CallOptions::class
296299
);
297300

298-
$res = $this->gapicClient->beginTransaction($beginTransactionRequest, $callOptions);
301+
try {
302+
$res = $this->gapicClient->beginTransaction($beginTransactionRequest, $callOptions);
303+
} catch (ApiException $ex) {
304+
throw $this->convertToGoogleException($ex);
305+
}
299306

300307
return base64_encode($res->getTransaction());
301308
}
@@ -357,7 +364,11 @@ public function allocateIds(array $keys, array $options = [])
357364
CallOptions::class
358365
);
359366

360-
$allocateIdsResponse = $this->gapicClient->allocateIds($allocateIdsRequest, $callOptions);
367+
try {
368+
$allocateIdsResponse = $this->gapicClient->allocateIds($allocateIdsRequest, $callOptions);
369+
} catch (ApiException $ex) {
370+
throw $this->convertToGoogleException($ex);
371+
}
361372

362373
/** @var ProtobufKey $responseKey */
363374
foreach ($allocateIdsResponse->getKeys() as $index => $responseKey) {
@@ -444,7 +455,11 @@ public function lookup(array $keys, array $options = []): array
444455
$lookupRequest->setReadOptions($readOptions);
445456
}
446457

447-
$lookupResponse = $this->gapicClient->lookup($lookupRequest, $callOptions);
458+
try {
459+
$lookupResponse = $this->gapicClient->lookup($lookupRequest, $callOptions);
460+
} catch (ApiException $ex) {
461+
throw $this->convertToGoogleException($ex);
462+
}
448463

449464
$result = [
450465
'result' => [],
@@ -590,7 +605,11 @@ public function runQuery(QueryInterface $query, array $options = []): EntityIter
590605
$runQueryRequest->setReadOptions($readOptions);
591606
}
592607

593-
$runQueryResponse = $this->gapicClient->runQuery($runQueryRequest, $callOptions);
608+
try {
609+
$runQueryResponse = $this->gapicClient->runQuery($runQueryRequest, $callOptions);
610+
} catch (ApiException $ex) {
611+
throw $this->convertToGoogleException($ex);
612+
}
594613

595614
// When executing a GQL Query, the server will compute a query object
596615
// and return it with the first response batch.
@@ -694,8 +713,12 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option
694713
$runAggregationQueryRequest->setReadOptions($readOptions);
695714
}
696715

697-
$runAggregationQueryResponse = $this->gapicClient
698-
->runAggregationQuery($runAggregationQueryRequest, $callOptions);
716+
try {
717+
$runAggregationQueryResponse = $this->gapicClient
718+
->runAggregationQuery($runAggregationQueryRequest, $callOptions);
719+
} catch (ApiException $ex) {
720+
throw $this->convertToGoogleException($ex);
721+
}
699722

700723
$res = $this->serializer->encodeMessage($runAggregationQueryResponse);
701724

@@ -744,7 +767,11 @@ public function commit(array $mutations, array $options = [])
744767
: MODE::TRANSACTIONAL
745768
);
746769

747-
$commitResponse = $this->gapicClient->commit($commitRequest, $callOptions);
770+
try {
771+
$commitResponse = $this->gapicClient->commit($commitRequest, $callOptions);
772+
} catch (ApiException $ex) {
773+
throw $this->convertToGoogleException($ex);
774+
}
748775

749776
return $this->serializer->encodeMessage($commitResponse);
750777
}
@@ -836,7 +863,11 @@ public function rollback(string $transactionId): void
836863
->setDatabaseId($this->databaseId)
837864
->setTransaction(base64_decode($transactionId));
838865

839-
$this->gapicClient->rollback($rollbackRequest);
866+
try {
867+
$this->gapicClient->rollback($rollbackRequest);
868+
} catch (ApiException $ex) {
869+
throw $this->convertToGoogleException($ex);
870+
}
840871
}
841872

842873
/**

Datastore/tests/Unit/OperationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
namespace Google\Cloud\Datastore\Tests\Unit;
1919

20+
use Google\ApiCore\ApiException;
2021
use Google\Cloud\Core\Testing\TestHelpers;
2122
use Google\Cloud\Core\Timestamp;
23+
use Google\Cloud\Core\Exception\FailedPreconditionException;
2224
use Google\Cloud\Datastore\Entity;
2325
use Google\Cloud\Datastore\EntityIterator;
2426
use Google\Cloud\Datastore\EntityMapper;
@@ -45,6 +47,7 @@
4547
use Google\Cloud\Datastore\V1\RunQueryRequest;
4648
use Google\Cloud\Datastore\V1\RunQueryResponse;
4749
use Google\Protobuf\Timestamp as ProtobufTimestamp;
50+
use Google\Rpc\Code;
4851
use InvalidArgumentException;
4952
use PHPUnit\Framework\TestCase;
5053
use Prophecy\Argument;
@@ -1133,4 +1136,27 @@ public function testLookupWithDatabaseIdOverride()
11331136
['databaseId' => 'otherDatabaseId']
11341137
);
11351138
}
1139+
1140+
public function testRunQueryApiExceptionConversion()
1141+
{
1142+
$this->expectException(FailedPreconditionException::class);
1143+
$this->expectExceptionMessage('Test exception');
1144+
1145+
$query = $this->prophesize(Query::class);
1146+
$query->queryObject()->willReturn([]);
1147+
$query->queryKey()->willReturn('query');
1148+
1149+
$this->gapicClient->runQuery(
1150+
Argument::type(RunQueryRequest::class),
1151+
Argument::type('array')
1152+
)->willThrow(new ApiException(
1153+
'Test exception',
1154+
Code::FAILED_PRECONDITION,
1155+
'FAILED_PRECONDITION'
1156+
));
1157+
1158+
$iterator = $this->operation->runQuery($query->reveal());
1159+
// The exception is thrown when we iterate
1160+
$iterator->current();
1161+
}
11361162
}

0 commit comments

Comments
 (0)