diff --git a/Datastore/src/Operation.php b/Datastore/src/Operation.php index 8fffbe8a0860..3198014f509a 100644 --- a/Datastore/src/Operation.php +++ b/Datastore/src/Operation.php @@ -17,9 +17,11 @@ namespace Google\Cloud\Datastore; +use Google\ApiCore\ApiException; use Google\ApiCore\Options\CallOptions; use Google\Cloud\Core\ApiHelperTrait; use Google\Cloud\Core\OptionsValidator; +use Google\Cloud\Core\RequestProcessorTrait; use Google\Cloud\Core\Timestamp; use Google\Cloud\Core\TimestampTrait; use Google\Cloud\Core\ValidateTrait; @@ -67,6 +69,7 @@ class Operation use ValidateTrait; use TimestampTrait; use ApiHelperTrait; + use RequestProcessorTrait; /** * @var Serializer @@ -295,7 +298,11 @@ public function beginTransaction(array $transactionOptions, array $options = []) CallOptions::class ); - $res = $this->gapicClient->beginTransaction($beginTransactionRequest, $callOptions); + try { + $res = $this->gapicClient->beginTransaction($beginTransactionRequest, $callOptions); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } return base64_encode($res->getTransaction()); } @@ -357,7 +364,11 @@ public function allocateIds(array $keys, array $options = []) CallOptions::class ); - $allocateIdsResponse = $this->gapicClient->allocateIds($allocateIdsRequest, $callOptions); + try { + $allocateIdsResponse = $this->gapicClient->allocateIds($allocateIdsRequest, $callOptions); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } /** @var ProtobufKey $responseKey */ foreach ($allocateIdsResponse->getKeys() as $index => $responseKey) { @@ -444,7 +455,11 @@ public function lookup(array $keys, array $options = []): array $lookupRequest->setReadOptions($readOptions); } - $lookupResponse = $this->gapicClient->lookup($lookupRequest, $callOptions); + try { + $lookupResponse = $this->gapicClient->lookup($lookupRequest, $callOptions); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } $result = [ 'result' => [], @@ -590,7 +605,11 @@ public function runQuery(QueryInterface $query, array $options = []): EntityIter $runQueryRequest->setReadOptions($readOptions); } - $runQueryResponse = $this->gapicClient->runQuery($runQueryRequest, $callOptions); + try { + $runQueryResponse = $this->gapicClient->runQuery($runQueryRequest, $callOptions); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } // When executing a GQL Query, the server will compute a query object // and return it with the first response batch. @@ -694,8 +713,12 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option $runAggregationQueryRequest->setReadOptions($readOptions); } - $runAggregationQueryResponse = $this->gapicClient - ->runAggregationQuery($runAggregationQueryRequest, $callOptions); + try { + $runAggregationQueryResponse = $this->gapicClient + ->runAggregationQuery($runAggregationQueryRequest, $callOptions); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } $res = $this->serializer->encodeMessage($runAggregationQueryResponse); @@ -744,7 +767,11 @@ public function commit(array $mutations, array $options = []) : MODE::TRANSACTIONAL ); - $commitResponse = $this->gapicClient->commit($commitRequest, $callOptions); + try { + $commitResponse = $this->gapicClient->commit($commitRequest, $callOptions); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } return $this->serializer->encodeMessage($commitResponse); } @@ -836,7 +863,11 @@ public function rollback(string $transactionId): void ->setDatabaseId($this->databaseId) ->setTransaction(base64_decode($transactionId)); - $this->gapicClient->rollback($rollbackRequest); + try { + $this->gapicClient->rollback($rollbackRequest); + } catch (ApiException $ex) { + throw $this->convertToGoogleException($ex); + } } /** diff --git a/Datastore/tests/Unit/OperationTest.php b/Datastore/tests/Unit/OperationTest.php index 0c83fc8508b6..6e9887aaf49f 100644 --- a/Datastore/tests/Unit/OperationTest.php +++ b/Datastore/tests/Unit/OperationTest.php @@ -17,8 +17,10 @@ namespace Google\Cloud\Datastore\Tests\Unit; +use Google\ApiCore\ApiException; use Google\Cloud\Core\Testing\TestHelpers; use Google\Cloud\Core\Timestamp; +use Google\Cloud\Core\Exception\FailedPreconditionException; use Google\Cloud\Datastore\Entity; use Google\Cloud\Datastore\EntityIterator; use Google\Cloud\Datastore\EntityMapper; @@ -45,6 +47,7 @@ use Google\Cloud\Datastore\V1\RunQueryRequest; use Google\Cloud\Datastore\V1\RunQueryResponse; use Google\Protobuf\Timestamp as ProtobufTimestamp; +use Google\Rpc\Code; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Prophecy\Argument; @@ -1133,4 +1136,27 @@ public function testLookupWithDatabaseIdOverride() ['databaseId' => 'otherDatabaseId'] ); } + + public function testRunQueryApiExceptionConversion() + { + $this->expectException(FailedPreconditionException::class); + $this->expectExceptionMessage('Test exception'); + + $query = $this->prophesize(Query::class); + $query->queryObject()->willReturn([]); + $query->queryKey()->willReturn('query'); + + $this->gapicClient->runQuery( + Argument::type(RunQueryRequest::class), + Argument::type('array') + )->willThrow(new ApiException( + 'Test exception', + Code::FAILED_PRECONDITION, + 'FAILED_PRECONDITION' + )); + + $iterator = $this->operation->runQuery($query->reveal()); + // The exception is thrown when we iterate + $iterator->current(); + } }