From 8a306450cfa4071619989c02e4de0a8587501135 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Mon, 20 Jan 2025 22:10:53 +0000 Subject: [PATCH 01/16] Add support for protobufErrors on gRPC --- src/ApiException.php | 36 +++++++++++++++++++++++++++++------- src/Serializer.php | 15 ++++++++++++++- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/ApiException.php b/src/ApiException.php index b66fff873..f72c3b2e9 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -46,6 +46,7 @@ class ApiException extends Exception private $metadata; private $basicMessage; private $decodedMetadataErrorInfo; + private array $protobufErrors; /** * ApiException constructor. @@ -62,7 +63,8 @@ public function __construct( string $message, int $code, ?string $status = null, - array $optionalArgs = [] + array $optionalArgs = [], + array $protobufErrors = [], ) { $optionalArgs += [ 'previous' => null, @@ -76,6 +78,7 @@ public function __construct( if ($this->metadata) { $this->decodedMetadataErrorInfo = self::decodeMetadataErrorInfo($this->metadata); } + $this->protobufErrors = $protobufErrors; } public function getStatus() @@ -137,6 +140,15 @@ public function getErrorInfoMetadata() return ($this->decodedMetadataErrorInfo) ? $this->decodedMetadataErrorInfo['errorInfoMetadata'] : null; } + /** + * Returns the unserialized errors + * @return array + */ + public function getUnserializedErrors(): array + { + return $this->protobufErrors; + } + /** * @param stdClass $status * @return ApiException @@ -144,11 +156,13 @@ public function getErrorInfoMetadata() public static function createFromStdClass(stdClass $status) { $metadata = property_exists($status, 'metadata') ? $status->metadata : null; + $errors = []; return self::create( $status->details, $status->code, $metadata, - Serializer::decodeMetadata((array) $metadata) + Serializer::decodeMetadata((array) $metadata, $errors), + $errors, ); } @@ -235,6 +249,7 @@ private static function containsErrorInfo(array $decodedMetadata) * @param int $rpcCode * @param iterable|null $metadata * @param array $decodedMetadata + * @param array $protobufErrors * @param Exception|null $previous * @return ApiException */ @@ -243,6 +258,7 @@ private static function create( int $rpcCode, $metadata, array $decodedMetadata, + array $protobufErrors = null, ?Exception $previous = null ) { $containsErrorInfo = self::containsErrorInfo($decodedMetadata); @@ -263,11 +279,17 @@ private static function create( $metadata = iterator_to_array($metadata); } - return new ApiException($message, $rpcCode, $rpcStatus, [ - 'previous' => $previous, - 'metadata' => $metadata, - 'basicMessage' => $basicMessage, - ]); + return new ApiException( + $message, + $rpcCode, + $rpcStatus, + [ + 'previous' => $previous, + 'metadata' => $metadata, + 'basicMessage' => $basicMessage, + ], + $protobufErrors + ); } /** diff --git a/src/Serializer.php b/src/Serializer.php index 8c1047ee1..610f52bbd 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -63,6 +63,15 @@ class Serializer 'google.rpc.errorinfo-bin' => \Google\Rpc\ErrorInfo::class, 'google.rpc.help-bin' => \Google\Rpc\Help::class, 'google.rpc.localizedmessage-bin' => \Google\Rpc\LocalizedMessage::class, + 'type.googleapis.com/google.rpc.RetryInfo' => \Google\Rpc\RetryInfo::class, + 'type.googleapis.com/google.rpc.DebugInfo' => \Google\Rpc\DebugInfo::class, + 'type.googleapis.com/google.rpc.QuotaFailure' => \Google\Rpc\QuotaFailure::class, + 'type.googleapis.com/google.rpc.BadRequest' => \Google\Rpc\BadRequest::class, + 'type.googleapis.com/google.rpc.RequestInfo' => \Google\Rpc\RequestInfo::class, + 'type.googleapis.com/google.rpc.ResourceInfo' => \Google\Rpc\ResourceInfo::class, + 'type.googleapis.com/google.rpc.ErrorInfo' => \Google\Rpc\ErrorInfo::class, + 'type.googleapis.com/google.rpc.Help' => \Google\Rpc\Help::class, + 'type.googleapis.com/google.rpc.LocalizedMessage' => \Google\Rpc\LocalizedMessage::class, ]; private $fieldTransformers; @@ -178,9 +187,10 @@ public static function serializeToPhpArray(Message $message) * Decode metadata received from gRPC status object * * @param array $metadata + * @param array $errors * @return array */ - public static function decodeMetadata(array $metadata) + public static function decodeMetadata(array $metadata, array &$errors = null) { if (count($metadata) == 0) { return []; @@ -199,6 +209,9 @@ public static function decodeMetadata(array $metadata) try { $message->mergeFromString($value); $decodedValue += self::serializeToPhpArray($message); + if (!is_null($errors)) { + $errors[] = $message; + } } catch (\Exception $e) { // We encountered an error trying to deserialize the data $decodedValue += [ From 25cbadb5dfc2241e62401f804bd7e1ff3ab79328 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Mon, 20 Jan 2025 22:19:58 +0000 Subject: [PATCH 02/16] Add support for Rest transport to surface protobuf errors --- src/ApiException.php | 5 ++++- src/Serializer.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ApiException.php b/src/ApiException.php index f72c3b2e9..13dc9bceb 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -179,11 +179,13 @@ public static function createFromApiResponse( ?array $metadata = null, ?Exception $previous = null ) { + $errors = []; return self::create( $basicMessage, $rpcCode, $metadata, - Serializer::decodeMetadata((array) $metadata), + Serializer::decodeMetadata((array) $metadata, $errors), + $errors, $previous ); } @@ -208,6 +210,7 @@ public static function createFromRestApiResponse( $rpcCode, $metadata, is_null($metadata) ? [] : $metadata, + Serializer::encodeMetadataToProtobufErrors($metadata), $previous ); } diff --git a/src/Serializer.php b/src/Serializer.php index 610f52bbd..24e70a272 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -163,6 +163,34 @@ public function decodeMessage($message, array $data) } } + /** + * Encodes decoded metadata to the Protobuf error type + * + * @param array metadata + * @return array + */ + public static function encodeMetadataToProtobufErrors(array $metadata): array + { + $result = []; + + foreach ($metadata as $error) { + $message = null; + $type = $error['@type']; + + if (!isset(self::$metadataKnownTypes[$type])) { + continue; + } + + $class = self::$metadataKnownTypes[$type]; + $message = new $class; + $jsonMessage = json_encode(array_diff($error, ['@type' => $error['@type']])); + $message->mergeFromJsonString($jsonMessage); + $result[] = $message; + } + + return $result; + } + /** * @param Message $message * @return string Json representation of $message From ae4972bd550676e4553efef4d328f8df6d2ee0f7 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Mon, 20 Jan 2025 22:57:02 +0000 Subject: [PATCH 03/16] Fix a bug with array_diff make use of array_diff_key --- src/ApiException.php | 4 ++-- src/Serializer.php | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ApiException.php b/src/ApiException.php index 13dc9bceb..d4b00337d 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -210,7 +210,7 @@ public static function createFromRestApiResponse( $rpcCode, $metadata, is_null($metadata) ? [] : $metadata, - Serializer::encodeMetadataToProtobufErrors($metadata), + Serializer::encodeMetadataToProtobufErrors($metadata ?? []), $previous ); } @@ -291,7 +291,7 @@ private static function create( 'metadata' => $metadata, 'basicMessage' => $basicMessage, ], - $protobufErrors + $protobufErrors ?? [] ); } diff --git a/src/Serializer.php b/src/Serializer.php index 24e70a272..d5ffd72d4 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -175,6 +175,11 @@ public static function encodeMetadataToProtobufErrors(array $metadata): array foreach ($metadata as $error) { $message = null; + + if (!isset($error['@type'])) { + continue; + } + $type = $error['@type']; if (!isset(self::$metadataKnownTypes[$type])) { @@ -183,7 +188,7 @@ public static function encodeMetadataToProtobufErrors(array $metadata): array $class = self::$metadataKnownTypes[$type]; $message = new $class; - $jsonMessage = json_encode(array_diff($error, ['@type' => $error['@type']])); + $jsonMessage = json_encode(array_diff_key($error, ['@type' => true])); $message->mergeFromJsonString($jsonMessage); $result[] = $message; } From 06a96bba4dcf904712600cdd0109cd528ca55bd6 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Tue, 21 Jan 2025 23:07:43 +0000 Subject: [PATCH 04/16] Add tests to ApiException --- tests/Unit/ApiExceptionTest.php | 135 ++++++++++++++++++++++++++++---- 1 file changed, 118 insertions(+), 17 deletions(-) diff --git a/tests/Unit/ApiExceptionTest.php b/tests/Unit/ApiExceptionTest.php index 6acccdb54..f060a8dfd 100644 --- a/tests/Unit/ApiExceptionTest.php +++ b/tests/Unit/ApiExceptionTest.php @@ -32,6 +32,7 @@ namespace Google\ApiCore\Tests\Unit; use Google\ApiCore\ApiException; +use Google\ApiCore\ApiStatus; use Google\Protobuf\Any; use Google\Protobuf\Duration; use Google\Rpc\BadRequest; @@ -70,12 +71,13 @@ public function testWithoutMetadata() $this->assertSame(Code::OK, $apiException->getCode()); $this->assertSame($expectedMessage, $apiException->getMessage()); $this->assertNull($apiException->getMetadata()); + $this->assertEmpty($apiException->getUnserializedErrors()); } /** * @dataProvider getMetadata */ - public function testWithMetadataWithoutErrorInfo($metadata, $metadataArray) + public function testWithMetadataWithoutErrorInfo($metadata, $metadataArray, $unserializedErrorsCount) { $status = new \stdClass(); $status->code = Code::OK; @@ -94,13 +96,14 @@ public function testWithMetadataWithoutErrorInfo($metadata, $metadataArray) $this->assertSame(Code::OK, $apiException->getCode()); $this->assertSame($expectedMessageWithoutErrorDetails, $apiException->getMessage()); $this->assertSame($metadata, $apiException->getMetadata()); + $this->assertCount($unserializedErrorsCount ,$apiException->getUnserializedErrors()); } /** * Test without ErrorInfo in Metadata * @dataProvider getMetadata */ - public function testCreateFromApiResponse($metadata, $metadataArray) + public function testCreateFromApiResponse($metadata, $metadataArray, $unserializedErrorsCount) { $basicMessage = 'testWithMetadata'; $code = Code::OK; @@ -118,6 +121,7 @@ public function testCreateFromApiResponse($metadata, $metadataArray) $this->assertSame(Code::OK, $apiException->getCode()); $this->assertSame($expectedMessage, $apiException->getMessage()); $this->assertSame($metadata, $apiException->getMetadata()); + $this->assertCount($unserializedErrorsCount ,$apiException->getUnserializedErrors()); } public function getMetadata() @@ -188,20 +192,30 @@ public function getMetadata() 'message' => '', ], ]; + + // Format: Error, metadataArray, unserialized errors count return [ - [['unknown-bin' => ['some-data-that-should-not-appear']], $unknownBinData], - [['ascii' => ['ascii-data']], $asciiData], - [['google.rpc.retryinfo-bin' => [$retryInfo->serializeToString()]], $retryInfoData], - [[ - 'google.rpc.retryinfo-bin' => [(new RetryInfo())->serializeToString()], - 'google.rpc.debuginfo-bin' => [(new DebugInfo())->serializeToString()], - 'google.rpc.quotafailure-bin' => [(new QuotaFailure())->serializeToString()], - 'google.rpc.badrequest-bin' => [(new BadRequest())->serializeToString()], - 'google.rpc.requestinfo-bin' => [(new RequestInfo())->serializeToString()], - 'google.rpc.resourceinfo-bin' => [(new ResourceInfo())->serializeToString()], - 'google.rpc.help-bin' => [(new Help())->serializeToString()], - 'google.rpc.localizedmessage-bin' => [(new LocalizedMessage())->serializeToString()], - ], $allKnownTypesData], + [['unknown-bin' => ['some-data-that-should-not-appear']], $unknownBinData, 0], + [['ascii' => ['ascii-data']], $asciiData, 0], + [ + ['google.rpc.retryinfo-bin' => [$retryInfo->serializeToString()]], + $retryInfoData, + 1 + ], + [ + [ + 'google.rpc.retryinfo-bin' => [(new RetryInfo())->serializeToString()], + 'google.rpc.debuginfo-bin' => [(new DebugInfo())->serializeToString()], + 'google.rpc.quotafailure-bin' => [(new QuotaFailure())->serializeToString()], + 'google.rpc.badrequest-bin' => [(new BadRequest())->serializeToString()], + 'google.rpc.requestinfo-bin' => [(new RequestInfo())->serializeToString()], + 'google.rpc.resourceinfo-bin' => [(new ResourceInfo())->serializeToString()], + 'google.rpc.help-bin' => [(new Help())->serializeToString()], + 'google.rpc.localizedmessage-bin' => [(new LocalizedMessage())->serializeToString()], + ], + $allKnownTypesData, + 8 + ] ]; } @@ -327,6 +341,68 @@ public function getMetadataWithErrorInfo() ]; } + public function testGetUnserializedErrors() + { + $metadata = [ + [ + '@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' => '', + ], + ]; + + $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()); + $this->assertCount(8, $apiException->getUnserializedErrors()); + $this->assertSame(null, $apiException->getReason()); + $this->assertSame(null, $apiException->getDomain()); + $this->assertSame(null, $apiException->getErrorInfoMetadata()); + } + /** * @dataProvider getRestMetadata */ @@ -633,6 +709,22 @@ public function buildRequestExceptions() 'message' => 'Ruh-roh.', ] ]; + $protoError = [ + 'error' => [ + 'code' => ApiStatus::INVALID_ARGUMENT, + 'message' => 'error', + 'status' => 'INVALID_ARGUMENT', + 'details' => [ + [[ + '@type' => 'type.googleapis.com/google.rpc.BadRequest', + 'fieldViolations' => [ + 'field' => 'target_language_code', + 'description' => 'Target language: invalid language' + ] + ]] + ] + ] + ]; $stream = RequestException::create( new Request('POST', 'http://www.example.com'), new Response( @@ -646,7 +738,7 @@ public function buildRequestExceptions() new Response( 404, [], - json_encode($error) + json_encode([$error]) ) ); unset($error['error']['message']); @@ -666,11 +758,20 @@ public function buildRequestExceptions() json_encode($error) ) ); + $withProtoError = RequestException::create( + new Request('POST', 'http://www.example.com'), + new Response( + 400, + [], + json_encode($protoError) + ) + ); return [ [$stream, true, Code::NOT_FOUND], [$unary, false, Code::NOT_FOUND], [$withoutErrorMessageStream, true, Code::NOT_FOUND], - [$withoutErrorMessageUnary, true, Code::NOT_FOUND] + [$withoutErrorMessageUnary, true, Code::NOT_FOUND], + [$withProtoError, false, Code::INVALID_ARGUMENT] ]; } } From eb7789d729a748b795d5bef248efc230c05130cc Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Wed, 22 Jan 2025 21:52:10 +0000 Subject: [PATCH 05/16] Add tests to SerializerTest --- tests/Unit/SerializerTest.php | 116 ++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/tests/Unit/SerializerTest.php b/tests/Unit/SerializerTest.php index 0d42e65ed..2797a5f33 100644 --- a/tests/Unit/SerializerTest.php +++ b/tests/Unit/SerializerTest.php @@ -38,6 +38,15 @@ use Google\Protobuf\ListValue; use Google\Protobuf\Struct; use Google\Protobuf\Value; +use Google\Rpc\BadRequest; +use Google\Rpc\DebugInfo; +use Google\Rpc\ErrorInfo; +use Google\Rpc\Help; +use Google\Rpc\LocalizedMessage; +use Google\Rpc\QuotaFailure; +use Google\Rpc\RequestInfo; +use Google\Rpc\ResourceInfo; +use Google\Rpc\RetryInfo; use Google\Rpc\Status; use Google\Type\Color; use PHPUnit\Framework\TestCase; @@ -152,6 +161,113 @@ public function testFieldMask() $this->verifySerializeAndDeserialize($message, $encodedMessage); } + public function testEncodeMetadataToProtobufErrors() + { + $allNonBinTypes = [ + [ + '@type' => 'type.googleapis.com/google.rpc.RetryInfo', + ], + [ + '@type' => 'type.googleapis.com/google.rpc.DebugInfo', + 'stackEntries' => [], + 'detail' => '' + ], + [ + '@type' => 'type.googleapis.com/google.rpc.QuotaFailure', + 'violations' => [], + ], + [ + '@type' => 'type.googleapis.com/google.rpc.BadRequest', + 'fieldViolations' => [] + ], + [ + '@type' => 'type.googleapis.com/google.rpc.RequestInfo', + 'requestId' => '', + 'servingData' => '', + ], + [ + '@type' => 'type.googleapis.com/google.rpc.ResourceInfo', + 'resourceType' => '', + 'resourceName' => '', + 'owner' => '', + 'description' => '', + ], + [ + '@type' => 'type.googleapis.com/google.rpc.ErrorInfo', + 'reason' => '', + 'domain' => '', + 'metadata' => [], + ], + [ + '@type' => 'type.googleapis.com/google.rpc.Help', + 'links' => [], + ], + [ + '@type' => 'type.googleapis.com/google.rpc.LocalizedMessage', + 'locale' => '', + 'message' => '', + ], + ]; + $encodedTypes = [ + new RetryInfo(), + new DebugInfo(), + new QuotaFailure(), + new BadRequest(), + new RequestInfo(), + new ResourceInfo(), + new ErrorInfo(), + new Help(), + new LocalizedMessage() + ]; + + $encodedErrors = Serializer::encodeMetadataToProtobufErrors($allNonBinTypes); + $this->assertCount(9, $encodedErrors); + $this->assertEquals($encodedTypes, $encodedErrors); + } + + public function testEncodeMetadataToProtobufErrorsNonKnownType() + { + $metadata = [ + [ + '@type' => 'unknown', + 'details' => 'Unknown error' + ] + ]; + + $encodedErrors = Serializer::encodeMetadataToProtobufErrors($metadata); + $this->assertCount(0, $encodedErrors); + } + + public function testDecodeMetadataReturnsErrorsWithArrayPointer() + { + $expectedError = new BadRequest(); + $metadata = [ + 'google.rpc.badrequest-bin' => [$expectedError->serializeToString()] + ]; + + $protobufErrors = []; + $expectedProtbufErrors = [ + $expectedError + ]; + + Serializer::decodeMetadata($metadata, $protobufErrors); + + $this->assertCount(1, $protobufErrors); + $this->assertEquals($expectedProtbufErrors, $protobufErrors); + } + + public function testDecodeMetadataDoesNotAddUnknownsToErrors() + { + $metadata = [ + 'unknown' => ['random string'] + ]; + + $protobufErrors = []; + Serializer::decodeMetadata($metadata, $protobufErrors); + + $this->assertCount(0, $protobufErrors); + } + public function testProperlyHandlesMessage() { $value = 'test'; From 606fa73c15f41f9dd73066b353911420068d2e33 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 19:46:13 +0000 Subject: [PATCH 06/16] Rename errors method to getErrorDetails --- src/ApiException.php | 2 +- tests/Unit/ApiExceptionTest.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ApiException.php b/src/ApiException.php index d4b00337d..cb78cf013 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -144,7 +144,7 @@ public function getErrorInfoMetadata() * Returns the unserialized errors * @return array */ - public function getUnserializedErrors(): array + public function getErrorDetails(): array { return $this->protobufErrors; } diff --git a/tests/Unit/ApiExceptionTest.php b/tests/Unit/ApiExceptionTest.php index f060a8dfd..3b78a2fc9 100644 --- a/tests/Unit/ApiExceptionTest.php +++ b/tests/Unit/ApiExceptionTest.php @@ -71,7 +71,7 @@ public function testWithoutMetadata() $this->assertSame(Code::OK, $apiException->getCode()); $this->assertSame($expectedMessage, $apiException->getMessage()); $this->assertNull($apiException->getMetadata()); - $this->assertEmpty($apiException->getUnserializedErrors()); + $this->assertEmpty($apiException->getErrorDetails()); } /** @@ -96,7 +96,7 @@ public function testWithMetadataWithoutErrorInfo($metadata, $metadataArray, $uns $this->assertSame(Code::OK, $apiException->getCode()); $this->assertSame($expectedMessageWithoutErrorDetails, $apiException->getMessage()); $this->assertSame($metadata, $apiException->getMetadata()); - $this->assertCount($unserializedErrorsCount ,$apiException->getUnserializedErrors()); + $this->assertCount($unserializedErrorsCount ,$apiException->getErrorDetails()); } /** @@ -121,7 +121,7 @@ public function testCreateFromApiResponse($metadata, $metadataArray, $unserializ $this->assertSame(Code::OK, $apiException->getCode()); $this->assertSame($expectedMessage, $apiException->getMessage()); $this->assertSame($metadata, $apiException->getMetadata()); - $this->assertCount($unserializedErrorsCount ,$apiException->getUnserializedErrors()); + $this->assertCount($unserializedErrorsCount ,$apiException->getErrorDetails()); } public function getMetadata() @@ -341,7 +341,7 @@ public function getMetadataWithErrorInfo() ]; } - public function testGetUnserializedErrors() + public function testGetErrorDetails() { $metadata = [ [ @@ -397,7 +397,7 @@ public function testGetUnserializedErrors() ], JSON_PRETTY_PRINT); $this->assertSame($expectedMessage, $apiException->getMessage()); - $this->assertCount(8, $apiException->getUnserializedErrors()); + $this->assertCount(8, $apiException->getErrorDetails()); $this->assertSame(null, $apiException->getReason()); $this->assertSame(null, $apiException->getDomain()); $this->assertSame(null, $apiException->getErrorInfoMetadata()); From 428c43576a82147751762adeb03d27f23297cb5d Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 19:53:37 +0000 Subject: [PATCH 07/16] Fix style problems --- src/ApiException.php | 2 +- src/Serializer.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ApiException.php b/src/ApiException.php index cb78cf013..3d496d07e 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -261,7 +261,7 @@ private static function create( int $rpcCode, $metadata, array $decodedMetadata, - array $protobufErrors = null, + ?array $protobufErrors = null, ?Exception $previous = null ) { $containsErrorInfo = self::containsErrorInfo($decodedMetadata); diff --git a/src/Serializer.php b/src/Serializer.php index d5ffd72d4..e389b800a 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -187,7 +187,7 @@ public static function encodeMetadataToProtobufErrors(array $metadata): array } $class = self::$metadataKnownTypes[$type]; - $message = new $class; + $message = new $class(); $jsonMessage = json_encode(array_diff_key($error, ['@type' => true])); $message->mergeFromJsonString($jsonMessage); $result[] = $message; From 8e7ea5fcbc5e39f584f615b15971031d9927fc7c Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 20:06:52 +0000 Subject: [PATCH 08/16] Add type annotation --- src/ApiException.php | 2 +- src/Serializer.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ApiException.php b/src/ApiException.php index 3d496d07e..a6c4efa4d 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -252,7 +252,7 @@ private static function containsErrorInfo(array $decodedMetadata) * @param int $rpcCode * @param iterable|null $metadata * @param array $decodedMetadata - * @param array $protobufErrors + * @param array|null $protobufErrors * @param Exception|null $previous * @return ApiException */ diff --git a/src/Serializer.php b/src/Serializer.php index e389b800a..92f321869 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -220,10 +220,10 @@ public static function serializeToPhpArray(Message $message) * Decode metadata received from gRPC status object * * @param array $metadata - * @param array $errors + * @param null|array $errors * @return array */ - public static function decodeMetadata(array $metadata, array &$errors = null) + public static function decodeMetadata(array $metadata, ?array &$errors = null) { if (count($metadata) == 0) { return []; From cc68ce024e3f29f2a1d813df62ed30842b1df241 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 20:15:57 +0000 Subject: [PATCH 09/16] Fix type annotation --- src/Serializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serializer.php b/src/Serializer.php index 92f321869..46f384988 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -166,7 +166,7 @@ public function decodeMessage($message, array $data) /** * Encodes decoded metadata to the Protobuf error type * - * @param array metadata + * @param array $metadata * @return array */ public static function encodeMetadataToProtobufErrors(array $metadata): array From 468f1dd01eb4d3bd33c32ba3a8abb7f2d0a43656 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 23:03:01 +0000 Subject: [PATCH 10/16] Add class for KnownTypes --- src/ApiException.php | 36 ++++++++++++++- src/KnownTypes.php | 64 +++++++++++++++++++++++++++ src/Serializer.php | 60 ++----------------------- tests/Unit/ApiExceptionTest.php | 42 +++++++++++++----- tests/Unit/SerializerTest.php | 77 --------------------------------- 5 files changed, 132 insertions(+), 147 deletions(-) create mode 100644 src/KnownTypes.php diff --git a/src/ApiException.php b/src/ApiException.php index a6c4efa4d..e16f5aa1b 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -210,7 +210,7 @@ public static function createFromRestApiResponse( $rpcCode, $metadata, is_null($metadata) ? [] : $metadata, - Serializer::encodeMetadataToProtobufErrors($metadata ?? []), + self::decodeMetadataToProtobufErrors($metadata ?? []), $previous ); } @@ -295,6 +295,40 @@ private static function create( ); } + /** + * Encodes decoded metadata to the Protobuf error type + * + * @param array $metadata + * @return array + */ + private static function decodeMetadataToProtobufErrors(array $metadata): array + { + $result = []; + Serializer::loadKnownMetadataTypes(); + + foreach ($metadata as $error) { + $message = null; + + if (!isset($error['@type'])) { + continue; + } + + $type = $error['@type']; + + if (!isset(KnownTypes::JSON_KNOWN_TYPES[$type])) { + continue; + } + + $class = KnownTypes::JSON_KNOWN_TYPES[$type]; + $message = new $class(); + $jsonMessage = json_encode(array_diff_key($error, ['@type' => true]), JSON_FORCE_OBJECT); + $message->mergeFromJsonString($jsonMessage); + $result[] = $message; + } + + return $result; + } + /** * @param Status $status * @return ApiException diff --git a/src/KnownTypes.php b/src/KnownTypes.php new file mode 100644 index 000000000..43bd7464b --- /dev/null +++ b/src/KnownTypes.php @@ -0,0 +1,64 @@ + \Google\Rpc\RetryInfo::class, + 'google.rpc.debuginfo-bin' => \Google\Rpc\DebugInfo::class, + 'google.rpc.quotafailure-bin' => \Google\Rpc\QuotaFailure::class, + 'google.rpc.badrequest-bin' => \Google\Rpc\BadRequest::class, + 'google.rpc.requestinfo-bin' => \Google\Rpc\RequestInfo::class, + 'google.rpc.resourceinfo-bin' => \Google\Rpc\ResourceInfo::class, + 'google.rpc.errorinfo-bin' => \Google\Rpc\ErrorInfo::class, + 'google.rpc.help-bin' => \Google\Rpc\Help::class, + 'google.rpc.localizedmessage-bin' => \Google\Rpc\LocalizedMessage::class, + ]; + + public const JSON_KNOWN_TYPES = [ + 'type.googleapis.com/google.rpc.RetryInfo' => \Google\Rpc\RetryInfo::class, + 'type.googleapis.com/google.rpc.DebugInfo' => \Google\Rpc\DebugInfo::class, + 'type.googleapis.com/google.rpc.QuotaFailure' => \Google\Rpc\QuotaFailure::class, + 'type.googleapis.com/google.rpc.BadRequest' => \Google\Rpc\BadRequest::class, + 'type.googleapis.com/google.rpc.RequestInfo' => \Google\Rpc\RequestInfo::class, + 'type.googleapis.com/google.rpc.ResourceInfo' => \Google\Rpc\ResourceInfo::class, + 'type.googleapis.com/google.rpc.ErrorInfo' => \Google\Rpc\ErrorInfo::class, + 'type.googleapis.com/google.rpc.Help' => \Google\Rpc\Help::class, + 'type.googleapis.com/google.rpc.LocalizedMessage' => \Google\Rpc\LocalizedMessage::class, + ]; + + public static function allKnownTypes(): array + { + return self::BINARY_KNOWN_TYPES + self::JSON_KNOWN_TYPES; + } +} \ No newline at end of file diff --git a/src/Serializer.php b/src/Serializer.php index 46f384988..5c4cf9923 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -53,27 +53,6 @@ class Serializer private static array $snakeCaseMap = []; private static array $camelCaseMap = []; - private static $metadataKnownTypes = [ - 'google.rpc.retryinfo-bin' => \Google\Rpc\RetryInfo::class, - 'google.rpc.debuginfo-bin' => \Google\Rpc\DebugInfo::class, - 'google.rpc.quotafailure-bin' => \Google\Rpc\QuotaFailure::class, - 'google.rpc.badrequest-bin' => \Google\Rpc\BadRequest::class, - 'google.rpc.requestinfo-bin' => \Google\Rpc\RequestInfo::class, - 'google.rpc.resourceinfo-bin' => \Google\Rpc\ResourceInfo::class, - 'google.rpc.errorinfo-bin' => \Google\Rpc\ErrorInfo::class, - 'google.rpc.help-bin' => \Google\Rpc\Help::class, - 'google.rpc.localizedmessage-bin' => \Google\Rpc\LocalizedMessage::class, - 'type.googleapis.com/google.rpc.RetryInfo' => \Google\Rpc\RetryInfo::class, - 'type.googleapis.com/google.rpc.DebugInfo' => \Google\Rpc\DebugInfo::class, - 'type.googleapis.com/google.rpc.QuotaFailure' => \Google\Rpc\QuotaFailure::class, - 'type.googleapis.com/google.rpc.BadRequest' => \Google\Rpc\BadRequest::class, - 'type.googleapis.com/google.rpc.RequestInfo' => \Google\Rpc\RequestInfo::class, - 'type.googleapis.com/google.rpc.ResourceInfo' => \Google\Rpc\ResourceInfo::class, - 'type.googleapis.com/google.rpc.ErrorInfo' => \Google\Rpc\ErrorInfo::class, - 'type.googleapis.com/google.rpc.Help' => \Google\Rpc\Help::class, - 'type.googleapis.com/google.rpc.LocalizedMessage' => \Google\Rpc\LocalizedMessage::class, - ]; - private $fieldTransformers; private $messageTypeTransformers; private $decodeFieldTransformers; @@ -163,39 +142,6 @@ public function decodeMessage($message, array $data) } } - /** - * Encodes decoded metadata to the Protobuf error type - * - * @param array $metadata - * @return array - */ - public static function encodeMetadataToProtobufErrors(array $metadata): array - { - $result = []; - - foreach ($metadata as $error) { - $message = null; - - if (!isset($error['@type'])) { - continue; - } - - $type = $error['@type']; - - if (!isset(self::$metadataKnownTypes[$type])) { - continue; - } - - $class = self::$metadataKnownTypes[$type]; - $message = new $class(); - $jsonMessage = json_encode(array_diff_key($error, ['@type' => true])); - $message->mergeFromJsonString($jsonMessage); - $result[] = $message; - } - - return $result; - } - /** * @param Message $message * @return string Json representation of $message @@ -235,8 +181,8 @@ public static function decodeMetadata(array $metadata, ?array &$errors = null) '@type' => $key, ]; if (self::hasBinaryHeaderSuffix($key)) { - if (isset(self::$metadataKnownTypes[$key])) { - $class = self::$metadataKnownTypes[$key]; + if (isset(KnownTypes::BINARY_KNOWN_TYPES[$key])) { + $class = KnownTypes::BINARY_KNOWN_TYPES[$key]; /** @var Message $message */ $message = new $class(); try { @@ -562,7 +508,7 @@ private static function getPhpArraySerializer() public static function loadKnownMetadataTypes() { - foreach (self::$metadataKnownTypes as $key => $class) { + foreach (KnownTypes::allKnownTypes() as $key => $class) { new $class(); } } diff --git a/tests/Unit/ApiExceptionTest.php b/tests/Unit/ApiExceptionTest.php index 3b78a2fc9..12a736a12 100644 --- a/tests/Unit/ApiExceptionTest.php +++ b/tests/Unit/ApiExceptionTest.php @@ -345,39 +345,47 @@ public function testGetErrorDetails() { $metadata = [ [ - '@type' => 'google.rpc.retryinfo-bin', + '@type' => 'type.googleapis.com/google.rpc.RetryInfo', ], [ - '@type' => 'google.rpc.debuginfo-bin', + '@type' => 'type.googleapis.com/google.rpc.DebugInfo', 'stackEntries' => [], 'detail' => '' ], [ - '@type' => 'google.rpc.quotafailure-bin', + '@type' => 'type.googleapis.com/google.rpc.QuotaFailure', 'violations' => [], ], [ - '@type' => 'google.rpc.badrequest-bin', + '@type' => 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations' => [] ], [ - '@type' => 'google.rpc.requestinfo-bin', + '@type' => 'type.googleapis.com/google.rpc.RequestInfo', 'requestId' => '', 'servingData' => '', ], [ - '@type' => 'google.rpc.resourceinfo-bin', + '@type' => 'type.googleapis.com/google.rpc.ResourceInfo', 'resourceType' => '', 'resourceName' => '', 'owner' => '', 'description' => '', ], [ - '@type' => 'google.rpc.help-bin', + '@type' => 'type.googleapis.com/google.rpc.ErrorInfo', + 'reason' => 'Error Info', + 'domain' => 'Error Info Domain', + 'metadata' => [ + 'test' => 'Error Info Test' + ], + ], + [ + '@type' => 'type.googleapis.com/google.rpc.Help', 'links' => [], ], [ - '@type' => 'google.rpc.localizedmessage-bin', + '@type' => 'type.googleapis.com/google.rpc.LocalizedMessage', 'locale' => '', 'message' => '', ], @@ -390,6 +398,11 @@ public function testGetErrorDetails() $apiException = ApiException::createFromRestApiResponse($basicMessage, $code, $metadata); $expectedMessage = json_encode([ + 'reason' => 'Error Info', + 'domain' => 'Error Info Domain', + 'errorInfoMetadata' => [ + 'test' => 'Error Info Test' + ], 'message' => $basicMessage, 'code' => $code, 'status' => $status, @@ -397,10 +410,15 @@ public function testGetErrorDetails() ], JSON_PRETTY_PRINT); $this->assertSame($expectedMessage, $apiException->getMessage()); - $this->assertCount(8, $apiException->getErrorDetails()); - $this->assertSame(null, $apiException->getReason()); - $this->assertSame(null, $apiException->getDomain()); - $this->assertSame(null, $apiException->getErrorInfoMetadata()); + $this->assertCount(9, $apiException->getErrorDetails()); + $this->assertSame('Error Info', $apiException->getReason()); + $this->assertSame('Error Info Domain', $apiException->getDomain()); + $this->assertSame( + [ + 'test' => 'Error Info Test' + ], + $apiException->getErrorInfoMetadata() + ); } /** diff --git a/tests/Unit/SerializerTest.php b/tests/Unit/SerializerTest.php index 2797a5f33..d8e0c227e 100644 --- a/tests/Unit/SerializerTest.php +++ b/tests/Unit/SerializerTest.php @@ -161,83 +161,6 @@ public function testFieldMask() $this->verifySerializeAndDeserialize($message, $encodedMessage); } - public function testEncodeMetadataToProtobufErrors() - { - $allNonBinTypes = [ - [ - '@type' => 'type.googleapis.com/google.rpc.RetryInfo', - ], - [ - '@type' => 'type.googleapis.com/google.rpc.DebugInfo', - 'stackEntries' => [], - 'detail' => '' - ], - [ - '@type' => 'type.googleapis.com/google.rpc.QuotaFailure', - 'violations' => [], - ], - [ - '@type' => 'type.googleapis.com/google.rpc.BadRequest', - 'fieldViolations' => [] - ], - [ - '@type' => 'type.googleapis.com/google.rpc.RequestInfo', - 'requestId' => '', - 'servingData' => '', - ], - [ - '@type' => 'type.googleapis.com/google.rpc.ResourceInfo', - 'resourceType' => '', - 'resourceName' => '', - 'owner' => '', - 'description' => '', - ], - [ - '@type' => 'type.googleapis.com/google.rpc.ErrorInfo', - 'reason' => '', - 'domain' => '', - 'metadata' => [], - ], - [ - '@type' => 'type.googleapis.com/google.rpc.Help', - 'links' => [], - ], - [ - '@type' => 'type.googleapis.com/google.rpc.LocalizedMessage', - 'locale' => '', - 'message' => '', - ], - ]; - $encodedTypes = [ - new RetryInfo(), - new DebugInfo(), - new QuotaFailure(), - new BadRequest(), - new RequestInfo(), - new ResourceInfo(), - new ErrorInfo(), - new Help(), - new LocalizedMessage() - ]; - - $encodedErrors = Serializer::encodeMetadataToProtobufErrors($allNonBinTypes); - $this->assertCount(9, $encodedErrors); - $this->assertEquals($encodedTypes, $encodedErrors); - } - - public function testEncodeMetadataToProtobufErrorsNonKnownType() - { - $metadata = [ - [ - '@type' => 'unknown', - 'details' => 'Unknown error' - ] - ]; - - $encodedErrors = Serializer::encodeMetadataToProtobufErrors($metadata); - $this->assertCount(0, $encodedErrors); - } - public function testDecodeMetadataReturnsErrorsWithArrayPointer() { $expectedError = new BadRequest(); From f672c8f4457bb761c2332f4f038eab22f9116ada Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 23:05:55 +0000 Subject: [PATCH 11/16] Rename the constants inside KnownTypes --- src/ApiException.php | 4 ++-- src/KnownTypes.php | 6 +++--- src/Serializer.php | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ApiException.php b/src/ApiException.php index e16f5aa1b..41dbe4859 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -315,11 +315,11 @@ private static function decodeMetadataToProtobufErrors(array $metadata): array $type = $error['@type']; - if (!isset(KnownTypes::JSON_KNOWN_TYPES[$type])) { + if (!isset(KnownTypes::JSON_TYPES[$type])) { continue; } - $class = KnownTypes::JSON_KNOWN_TYPES[$type]; + $class = KnownTypes::JSON_TYPES[$type]; $message = new $class(); $jsonMessage = json_encode(array_diff_key($error, ['@type' => true]), JSON_FORCE_OBJECT); $message->mergeFromJsonString($jsonMessage); diff --git a/src/KnownTypes.php b/src/KnownTypes.php index 43bd7464b..7dfea457a 100644 --- a/src/KnownTypes.php +++ b/src/KnownTypes.php @@ -33,7 +33,7 @@ class KnownTypes { - public const BINARY_KNOWN_TYPES = [ + public const GRPC_TYPES = [ 'google.rpc.retryinfo-bin' => \Google\Rpc\RetryInfo::class, 'google.rpc.debuginfo-bin' => \Google\Rpc\DebugInfo::class, 'google.rpc.quotafailure-bin' => \Google\Rpc\QuotaFailure::class, @@ -45,7 +45,7 @@ class KnownTypes 'google.rpc.localizedmessage-bin' => \Google\Rpc\LocalizedMessage::class, ]; - public const JSON_KNOWN_TYPES = [ + public const JSON_TYPES = [ 'type.googleapis.com/google.rpc.RetryInfo' => \Google\Rpc\RetryInfo::class, 'type.googleapis.com/google.rpc.DebugInfo' => \Google\Rpc\DebugInfo::class, 'type.googleapis.com/google.rpc.QuotaFailure' => \Google\Rpc\QuotaFailure::class, @@ -59,6 +59,6 @@ class KnownTypes public static function allKnownTypes(): array { - return self::BINARY_KNOWN_TYPES + self::JSON_KNOWN_TYPES; + return self::GRPC_TYPES + self::JSON_TYPES; } } \ No newline at end of file diff --git a/src/Serializer.php b/src/Serializer.php index 5c4cf9923..c9ddc4011 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -181,8 +181,8 @@ public static function decodeMetadata(array $metadata, ?array &$errors = null) '@type' => $key, ]; if (self::hasBinaryHeaderSuffix($key)) { - if (isset(KnownTypes::BINARY_KNOWN_TYPES[$key])) { - $class = KnownTypes::BINARY_KNOWN_TYPES[$key]; + if (isset(KnownTypes::GRPC_TYPES[$key])) { + $class = KnownTypes::GRPC_TYPES[$key]; /** @var Message $message */ $message = new $class(); try { From 8c57c660087281584bfa9070aecce4b79d4cccc9 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Thu, 23 Jan 2025 23:09:36 +0000 Subject: [PATCH 12/16] Fix style problems --- src/KnownTypes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/KnownTypes.php b/src/KnownTypes.php index 7dfea457a..53a4302f9 100644 --- a/src/KnownTypes.php +++ b/src/KnownTypes.php @@ -61,4 +61,4 @@ public static function allKnownTypes(): array { return self::GRPC_TYPES + self::JSON_TYPES; } -} \ No newline at end of file +} From bc25ba9d2c9b359b265df8fc192a7b6e82eaeb7a Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Fri, 24 Jan 2025 00:00:38 +0000 Subject: [PATCH 13/16] Json encode flag change --- src/ApiException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApiException.php b/src/ApiException.php index 41dbe4859..060ca33fd 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -321,7 +321,7 @@ private static function decodeMetadataToProtobufErrors(array $metadata): array $class = KnownTypes::JSON_TYPES[$type]; $message = new $class(); - $jsonMessage = json_encode(array_diff_key($error, ['@type' => true]), JSON_FORCE_OBJECT); + $jsonMessage = json_encode(array_diff_key($error, ['@type' => true])); $message->mergeFromJsonString($jsonMessage); $result[] = $message; } From a795c1f10a4ddd7ffeac4650ea1713652ffbbbd9 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Fri, 24 Jan 2025 00:06:57 +0000 Subject: [PATCH 14/16] Update JSON flag --- src/ApiException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApiException.php b/src/ApiException.php index 060ca33fd..41dbe4859 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -321,7 +321,7 @@ private static function decodeMetadataToProtobufErrors(array $metadata): array $class = KnownTypes::JSON_TYPES[$type]; $message = new $class(); - $jsonMessage = json_encode(array_diff_key($error, ['@type' => true])); + $jsonMessage = json_encode(array_diff_key($error, ['@type' => true]), JSON_FORCE_OBJECT); $message->mergeFromJsonString($jsonMessage); $result[] = $message; } From c09043e75a620645bf52662a2658a5d99f49abd2 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Fri, 24 Jan 2025 00:34:17 +0000 Subject: [PATCH 15/16] Fix the malformed RetryInfo --- src/ApiException.php | 2 +- tests/Unit/ApiExceptionTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ApiException.php b/src/ApiException.php index 41dbe4859..060ca33fd 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -321,7 +321,7 @@ private static function decodeMetadataToProtobufErrors(array $metadata): array $class = KnownTypes::JSON_TYPES[$type]; $message = new $class(); - $jsonMessage = json_encode(array_diff_key($error, ['@type' => true]), JSON_FORCE_OBJECT); + $jsonMessage = json_encode(array_diff_key($error, ['@type' => true])); $message->mergeFromJsonString($jsonMessage); $result[] = $message; } diff --git a/tests/Unit/ApiExceptionTest.php b/tests/Unit/ApiExceptionTest.php index 12a736a12..0fe6dc202 100644 --- a/tests/Unit/ApiExceptionTest.php +++ b/tests/Unit/ApiExceptionTest.php @@ -346,6 +346,7 @@ public function testGetErrorDetails() $metadata = [ [ '@type' => 'type.googleapis.com/google.rpc.RetryInfo', + 'retry_delay' => '1s' ], [ '@type' => 'type.googleapis.com/google.rpc.DebugInfo', From 886800325b0eb0ae4fe0ef7a93198fb359348dc0 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Wed, 5 Feb 2025 23:27:33 +0000 Subject: [PATCH 16/16] Add missing type for PrecoditionFailure --- src/KnownTypes.php | 4 +++- src/Serializer.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/KnownTypes.php b/src/KnownTypes.php index 53a4302f9..e092c5fa5 100644 --- a/src/KnownTypes.php +++ b/src/KnownTypes.php @@ -1,6 +1,6 @@ \Google\Rpc\ErrorInfo::class, 'google.rpc.help-bin' => \Google\Rpc\Help::class, 'google.rpc.localizedmessage-bin' => \Google\Rpc\LocalizedMessage::class, + 'google.rpc.preconditionfailure-bin' => \Google\Rpc\PreconditionFailure::class, ]; public const JSON_TYPES = [ @@ -55,6 +56,7 @@ class KnownTypes 'type.googleapis.com/google.rpc.ErrorInfo' => \Google\Rpc\ErrorInfo::class, 'type.googleapis.com/google.rpc.Help' => \Google\Rpc\Help::class, 'type.googleapis.com/google.rpc.LocalizedMessage' => \Google\Rpc\LocalizedMessage::class, + 'type.googleapis.com/google.rpc.PreconditionFailure' => \Google\Rpc\PreconditionFailure::class, ]; public static function allKnownTypes(): array diff --git a/src/Serializer.php b/src/Serializer.php index c9ddc4011..355038bce 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -508,7 +508,7 @@ private static function getPhpArraySerializer() public static function loadKnownMetadataTypes() { - foreach (KnownTypes::allKnownTypes() as $key => $class) { + foreach (KnownTypes::GRPC_TYPES as $key => $class) { new $class(); } }