Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify exceptions when a whitelist may be present #482

Merged
merged 4 commits into from
May 22, 2017
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
12 changes: 11 additions & 1 deletion src/Core/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@
*/
class NotFoundException extends ServiceException
{

/**
* Allows overriding message for injection of Whitelist Notice.
*
* @param string $message the new message
* @return void
* @access private
*/
public function setMessage($message)
{
$this->message = $message;
}
}
1 change: 1 addition & 0 deletions src/Core/GrpcRequestWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private function convertToGoogleException(ApiException $ex)
break;

case Grpc\STATUS_NOT_FOUND:
case Grpc\STATUS_UNIMPLEMENTED:
$exception = Exception\NotFoundException::class;
break;

Expand Down
17 changes: 14 additions & 3 deletions src/Core/GrpcTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

use DateTime;
use DateTimeZone;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\Cache\MemoryCacheItemPool;
use Google\Auth\FetchAuthTokenCache;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Core\GrpcRequestWrapper;
use google\protobuf;

Expand All @@ -31,6 +32,7 @@
trait GrpcTrait
{
use ArrayTrait;
use WhitelistTrait;

/**
* @var GrpcRequestWrapper Wrapper used to handle sending requests to the
Expand All @@ -53,17 +55,26 @@ public function setRequestWrapper(GrpcRequestWrapper $requestWrapper)
*
* @param callable $request
* @param array $args
* @param bool $whitelisted
* @return \Generator|array
*/
public function send(callable $request, array $args)
public function send(callable $request, array $args, $whitelisted = false)
{
$requestOptions = $this->pluckArray([
'grpcOptions',
'retries',
'requestTimeout'
], $args[count($args) - 1]);

return $this->requestWrapper->send($request, $args, $requestOptions);
try {
return $this->requestWrapper->send($request, $args, $requestOptions);
} catch (NotFoundException $e) {
if ($whitelisted) {
throw $this->modifyWhitelistedError($e);
}

throw $e;
}
}

/**
Expand Down
28 changes: 20 additions & 8 deletions src/Core/RestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

namespace Google\Cloud\Core;

use Google\Cloud\Core\Exception\NotFoundException;

/**
* Provides shared functionality for REST service implementations.
*/
trait RestTrait
{
use ArrayTrait;
use JsonTrait;
use WhitelistTrait;

/**
* @var RequestBuilder Builds PSR7 requests from a service definition.
Expand Down Expand Up @@ -64,22 +67,31 @@ public function setRequestWrapper(RequestWrapper $requestWrapper)
* @param string $resource The resource type used for the request.
* @param string $method The method used for the request.
* @param array $options [optional] Options used to build out the request.
* @param array $whitelisted [optional]
* @return array
*/
public function send($resource, $method, array $options = [])
public function send($resource, $method, array $options = [], $whitelisted = false)
{
$requestOptions = $this->pluckArray([
'restOptions',
'retries',
'requestTimeout'
], $options);

return json_decode(
$this->requestWrapper->send(
$this->requestBuilder->build($resource, $method, $options),
$requestOptions
)->getBody(),
true
);
try {
return json_decode(
$this->requestWrapper->send(
$this->requestBuilder->build($resource, $method, $options),
$requestOptions
)->getBody(),
true
);
} catch (NotFoundException $e) {
if ($whitelisted) {
throw $this->modifyWhitelistedError($e);
}

throw $e;
}
}
}
39 changes: 39 additions & 0 deletions src/Core/WhitelistTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core;

use Google\Cloud\Core\Exception\NotFoundException;

/**
* Manages exceptions for requests which may have whitelist restrictions.
*/
trait WhitelistTrait
{
/**
* Modify the error message of a whitelisted exception.
*
* @param NotFoundException $e The exception.
* @return NotFoundException
*/
private function modifyWhitelistedError(NotFoundException $e)
{
$e->setMessage('NOTE: Error may be due to Whitelist Restriction. ' . $e->getMessage());

return $e;
}
}
12 changes: 8 additions & 4 deletions src/PubSub/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,33 +288,36 @@ public function acknowledge(array $args)
*/
public function listSnapshots(array $args)
{
$whitelisted = true;
return $this->send([$this->subscriberClient, 'listSnapshots'], [
$this->pluck('project', $args),
$args
]);
], $whitelisted);
}

/**
* @param array $args
*/
public function createSnapshot(array $args)
{
$whitelisted = true;
return $this->send([$this->subscriberClient, 'createSnapshot'], [
$this->pluck('name', $args),
$this->pluck('subscription', $args),
$args
]);
], $whitelisted);
}

/**
* @param array $args
*/
public function deleteSnapshot(array $args)
{
$whitelisted = true;
return $this->send([$this->subscriberClient, 'deleteSnapshot'], [
$this->pluck('snapshot', $args),
$args
]);
], $whitelisted);
}

/**
Expand All @@ -327,10 +330,11 @@ public function seek(array $args)
$args['time'] = (new protobuf\Timestamp)->deserialize($time, $this->codec);
}

$whitelisted = true;
return $this->send([$this->subscriberClient, 'seek'], [
$this->pluck('subscription', $args),
$args
]);
], $whitelisted);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/PubSub/Connection/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,31 +214,35 @@ public function acknowledge(array $args)
*/
public function listSnapshots(array $args)
{
return $this->send('snapshots', 'list', $args);
$whitelisted = true;
return $this->send('snapshots', 'list', $args, $whitelisted);
}

/**
* @param array $args
*/
public function createSnapshot(array $args)
{
return $this->send('snapshots', 'create', $args);
$whitelisted = true;
return $this->send('snapshots', 'create', $args, $whitelisted);
}

/**
* @param array $args
*/
public function deleteSnapshot(array $args)
{
return $this->send('snapshots', 'delete', $args);
$whitelisted = true;
return $this->send('snapshots', 'delete', $args, $whitelisted);
}

/**
* @param array $args
*/
public function seek(array $args)
{
return $this->send('subscriptions', 'seek', $args);
$whitelisted = true;
return $this->send('subscriptions', 'seek', $args, $whitelisted);
}

/**
Expand Down
Loading