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

fix: update REST transport error details #352

Merged
merged 19 commits into from
Dec 1, 2021
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
26 changes: 25 additions & 1 deletion src/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ public static function createFromApiResponse(
);
}

/**
* For REST-based responses, the metadata does not need to be decoded.
*
* @param string $basicMessage
* @param int $rpcCode
* @param array|null $metadata
* @param \Exception $previous
* @return ApiException
*/
public static function createFromRestApiResponse(
$basicMessage,
$rpcCode,
array $metadata = null,
\Exception $previous = null
) {
return self::create(
$basicMessage,
$rpcCode,
$metadata,
is_null($metadata) ? [] : $metadata,
$previous
);
}

/**
* Construct an ApiException with a useful message, including decoded metadata.
*
Expand Down Expand Up @@ -184,7 +208,7 @@ public static function createFromRequestException(RequestException $ex, $isStrea
? ApiStatus::rpcCodeFromStatus($error['status'])
: $ex->getCode();
$metadata = isset($error['details']) ? $error['details'] : null;
return static::createFromApiResponse($basicMessage, $code, $metadata);
return static::createFromRestApiResponse($basicMessage, $code, $metadata);
}
// Use the RPC code instead of the HTTP Status Code.
$code = ApiStatus::rpcCodeFromHttpStatusCode($res->getStatusCode());
Expand Down
110 changes: 105 additions & 5 deletions tests/Tests/Unit/ApiExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public function getMetadata()
/**
* @dataProvider getMetadata
*/
public function testCreateFromApiResponse($metadata, $metadataArray) {
public function testCreateFromApiResponse($metadata, $metadataArray)
{
$basicMessage = 'testWithMetadata';
$code = Code::OK;
$status = 'OK';
Expand All @@ -203,10 +204,106 @@ public function testCreateFromApiResponse($metadata, $metadataArray) {
$this->assertSame($metadata, $apiException->getMetadata());
}

public function getRestMetadata()
{
$unknownBinData = [
[
'@type' => 'unknown-bin',
'data' => '<Unknown Binary Data>'
]
];
$asciiData = [
[
'@type' => 'ascii',
'data' => 'ascii-data'
]
];
$retryInfoData = [
[
'@type' => 'google.rpc.retryinfo-bin',
'retryDelay' => [
'seconds' => 1,
'nanos' => 2,
],
]
];
$allKnownTypesData = [
[
'@type' => 'google.rpc.retryinfo-bin',
],
[
'@type' => 'google.rpc.debuginfo-bin',
"stackEntries" => [],
"detail" => ""
],
[
'@type' => 'google.rpc.quotafailure-bin',
'violations' => [],
],
[
'@type' => 'google.rpc.badrequest-bin',
'fieldViolations' => []
],
[
'@type' => 'google.rpc.requestinfo-bin',
'requestId' => '',
'servingData' => '',
],
[
'@type' => 'google.rpc.resourceinfo-bin',
'resourceType' => '',
'resourceName' => '',
'owner' => '',
'description' => '',
],
[
'@type' => 'google.rpc.help-bin',
'links' => [],
],
[
'@type' => 'google.rpc.localizedmessage-bin',
'locale' => '',
'message' => '',
],
];

return [
[
alicejli marked this conversation as resolved.
Show resolved Hide resolved
[[]],
[[null]],
[$unknownBinData],
[$asciiData],
[$allKnownTypesData]
]
];
}

/**
* @dataProvider getRestMetadata
*/
public function testCreateFromRestApiResponse($metadata)
{
$basicMessage = 'testWithRestMetadata';
$code = Code::OK;
$status = 'OK';

$apiException = ApiException::createFromRestApiResponse($basicMessage, $code, $metadata);

$expectedMessage = json_encode([
'message' => $basicMessage,
'code' => $code,
'status' => $status,
'details' => $metadata
], JSON_PRETTY_PRINT);

$this->assertSame($expectedMessage, $apiException->getMessage());
}

/**
* @dataProvider getRpcStatusData
*/
public function testCreateFromRpcStatus($status, $expectedApiException) {
public function testCreateFromRpcStatus($status, $expectedApiException)
{
$actualApiException = ApiException::createFromRpcStatus($status);
$this->assertEquals($expectedApiException, $actualApiException);
}
Expand Down Expand Up @@ -238,7 +335,11 @@ public function getRpcStatusData()
return [
[
$status,
new ApiException($expectedMessage, Code::OK, 'OK', [
new ApiException(
$expectedMessage,
Code::OK,
'OK',
[
'metadata' => $status->getDetails(),
'basicMessage' => $status->getMessage(),
]
Expand All @@ -252,13 +353,12 @@ public function getRpcStatusData()
*/
public function testCreateFromRequestException($re, $stream, $expectedCode)
{

$ae = ApiException::createFromRequestException($re, $stream);
$this->assertSame($expectedCode, $ae->getCode());
}

public function buildRequestExceptions()
{
{
$error = [
'error' => [
'status' => 'NOT_FOUND',
Expand Down
9 changes: 6 additions & 3 deletions tests/Tests/Unit/Transport/RestTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use Google\ApiCore\Testing\MockRequest;
use Google\ApiCore\Testing\MockResponse;
use Google\ApiCore\Transport\RestTransport;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Protobuf\Any;
use Google\Rpc\ErrorInfo;
Expand Down Expand Up @@ -426,7 +425,9 @@ public function testAudienceOption()
$credentialsWrapper = $this->prophesize(CredentialsWrapper::class);
$credentialsWrapper->getAuthorizationHeaderCallback('an-audience')
->shouldBeCalledOnce()
->willReturn(function() { return []; });
->willReturn(function () {
return [];
});

$options = [
'audience' => 'an-audience',
Expand Down Expand Up @@ -465,7 +466,9 @@ public function testNonArrayAuthorizationHeaderThrowsException()
$credentialsWrapper = $this->prophesize(CredentialsWrapper::class);
$credentialsWrapper->getAuthorizationHeaderCallback(null)
->shouldBeCalledOnce()
->willReturn(function() { return ''; });
->willReturn(function () {
return '';
});

$options = [
'credentialsWrapper' => $credentialsWrapper->reveal(),
Expand Down