From 4d5e5a7c1c04297bbf240e6e3cc00d31d04c4f38 Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Wed, 18 Jul 2018 16:42:12 +0200 Subject: [PATCH 1/3] [FX] Also add throwing values in rejection callbacks --- src/Registry/BlockingRegistry.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Registry/BlockingRegistry.php b/src/Registry/BlockingRegistry.php index a2b522d..65da920 100644 --- a/src/Registry/BlockingRegistry.php +++ b/src/Registry/BlockingRegistry.php @@ -109,14 +109,14 @@ public function latestVersion(string $subject, callable $requestCallback = null) */ private function addExceptionThrowCallableToPromise(PromiseInterface $promise): PromiseInterface { - return $promise->then( - function ($value) { - if ($value instanceof \Exception) { - throw $value; - } - - return $value; + $throwingValueFunction = function ($value) { + if ($value instanceof \Exception) { + throw $value; } - ); + + return $value; + }; + + return $promise->then($throwingValueFunction, $throwingValueFunction); } } From 4a56d933648381e08c477669b23876f1de24ff5c Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Wed, 18 Jul 2018 17:14:15 +0200 Subject: [PATCH 2/3] [FX] Also add throwing values in rejection callbacks for CachedRegistry --- src/Registry/CachedRegistry.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Registry/CachedRegistry.php b/src/Registry/CachedRegistry.php index 40c9acd..b9b4014 100644 --- a/src/Registry/CachedRegistry.php +++ b/src/Registry/CachedRegistry.php @@ -169,16 +169,16 @@ public function latestVersion(string $subject, callable $requestCallback = null) return $this->registry->latestVersion($subject, $requestCallback); } - - /** - * {@inheritdoc} - */ private function applyValueHandlers($value, callable $promiseHandler, callable $valueHandler) { if ($value instanceof PromiseInterface) { return $promiseHandler($value); } + if ($value instanceof \Exception) { + throw $value; + } + return $valueHandler($value); } From b3f7b201fe2ad15f2c1d00ca219edabbecdf198a Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Wed, 18 Jul 2018 17:46:34 +0200 Subject: [PATCH 3/3] [FX] Use correct stream string access in `ExceptionMap` --- src/Exception/ExceptionMap.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Exception/ExceptionMap.php b/src/Exception/ExceptionMap.php index 6a1bcfe..b5aa258 100644 --- a/src/Exception/ExceptionMap.php +++ b/src/Exception/ExceptionMap.php @@ -63,18 +63,29 @@ private function guardAgainstMissingResponse(RequestException $exception): Respo private function guardAgainstMissingErrorCode(ResponseInterface $response): array { - $decodedBody = \GuzzleHttp\json_decode($response->getBody()->getContents(), true); - - if (!array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) { + try { + $decodedBody = \GuzzleHttp\json_decode((string) $response->getBody(), true); + + if (!\array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) { + throw new \RuntimeException( + sprintf( + 'Invalid message body received - cannot find "error_code" field in response body "%s"', + (string) $response->getBody() + ) + ); + } + + return $decodedBody; + } catch (\Exception $e) { throw new \RuntimeException( - sprintf( + \sprintf( 'Invalid message body received - cannot find "error_code" field in response body "%s"', - $response->getBody()->getContents() - ) + (string) $response->getBody() + ), + $e->getCode(), + $e ); } - - return $decodedBody; } private function mapErrorCodeToException($errorCode, $errorMessage)