Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions Datastore/src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +69,7 @@ class Operation
use ValidateTrait;
use TimestampTrait;
use ApiHelperTrait;
use RequestProcessorTrait;

/**
* @var Serializer
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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' => [],
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions Datastore/tests/Unit/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}