Skip to content

Commit 4d232ec

Browse files
authored
[FX] Use correct way of retrieving the contents of a Guzzle stream (#13)
1 parent 0569059 commit 4d232ec

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/Registry/PromisingRegistry.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function register(string $subject, AvroSchema $schema, callable $requestC
5252
$request = registerNewSchemaVersionWithSubjectRequest((string) $schema, $subject);
5353

5454
$onFulfilled = function (ResponseInterface $response) {
55-
$schemaId = \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['id'];
55+
$schemaId = $this->getJsonFromResponseBody($response)['id'];
5656

5757
return $schemaId;
5858
};
@@ -70,7 +70,7 @@ public function schemaId(string $subject, AvroSchema $schema, callable $requestC
7070
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
7171

7272
$onFulfilled = function (ResponseInterface $response) {
73-
$decodedResponse = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
73+
$decodedResponse = $this->getJsonFromResponseBody($response);
7474

7575
return $decodedResponse['id'];
7676
};
@@ -89,7 +89,7 @@ public function schemaForId(int $schemaId, callable $requestCallback = null): Pr
8989

9090
$onFulfilled = function (ResponseInterface $response) {
9191
$schema = AvroSchema::parse(
92-
\GuzzleHttp\json_decode($response->getBody()->getContents(), true)['schema']
92+
$this->getJsonFromResponseBody($response)['schema']
9393
);
9494

9595
return $schema;
@@ -109,7 +109,7 @@ public function schemaForSubjectAndVersion(string $subject, int $version, callab
109109

110110
$onFulfilled = function (ResponseInterface $response) {
111111
$schema = AvroSchema::parse(
112-
\GuzzleHttp\json_decode($response->getBody()->getContents(), true)['schema']
112+
$this->getJsonFromResponseBody($response)['schema']
113113
);
114114

115115
return $schema;
@@ -128,7 +128,7 @@ public function schemaVersion(string $subject, AvroSchema $schema, callable $req
128128
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
129129

130130
$onFulfilled = function (ResponseInterface $response) {
131-
return \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['version'];
131+
return $this->getJsonFromResponseBody($response)['version'];
132132
};
133133

134134
return $this->makeRequest($request, $onFulfilled, $requestCallback);
@@ -145,7 +145,7 @@ public function latestVersion(string $subject, callable $requestCallback = null)
145145

146146
$onFulfilled = function (ResponseInterface $response) {
147147
$schema = AvroSchema::parse(
148-
\GuzzleHttp\json_decode($response->getBody()->getContents(), true)['schema']
148+
$this->getJsonFromResponseBody($response)['schema']
149149
);
150150

151151
return $schema;
@@ -167,4 +167,19 @@ private function makeRequest(RequestInterface $request, callable $onFulfilled, c
167167
->sendAsync(null !== $requestCallback ? $requestCallback($request) : $request)
168168
->then($onFulfilled, $this->rejectedCallback);
169169
}
170+
171+
private function getJsonFromResponseBody(ResponseInterface $response): array
172+
{
173+
$body = (string) $response->getBody();
174+
175+
try {
176+
return \GuzzleHttp\json_decode($body, true);
177+
} catch (\InvalidArgumentException $e) {
178+
throw new \InvalidArgumentException(
179+
\sprintf('%s - with content "%s"', $e->getMessage(), $body),
180+
$e->getCode(),
181+
$e
182+
);
183+
}
184+
}
170185
}

0 commit comments

Comments
 (0)