Skip to content

Commit cf81fd2

Browse files
committed
- improved error consistency
- header name normalization
1 parent 8c2ecb9 commit cf81fd2

3 files changed

Lines changed: 10 additions & 15 deletions

File tree

Client/CurlClient.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public function read(): Response
7070
\curl_getinfo($resource, CURLINFO_RESPONSE_CODE),
7171
$this->responseHeaders);
7272
} catch (\TypeError $e) {
73-
return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY,
74-
$e->getMessage());
73+
return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY, $e->getMessage());
7574
} catch (\Throwable $e) {
7675
return $this->getPhpError(HttpStatus::INTERNAL_SERVER_ERROR, $e->getMessage());
7776
} finally {
@@ -158,7 +157,7 @@ protected function prepareRequestBody(): void
158157
if (0 === $this->encoding) {
159158
$this->options[CURLOPT_POSTFIELDS] = $this->stream->getContents();
160159
} elseif ($content = \json_decode($this->stream->getContents() ?: '[]', true)) {
161-
$this->normalizeHeader('Content-type', self::X_WWW_FORM_URLENCODED, true);
160+
$this->normalizeHeader('Content-Type', self::X_WWW_FORM_URLENCODED, true);
162161
$this->options[CURLOPT_POSTFIELDS] = \http_build_query($content, null, '&', $this->encoding);
163162
}
164163
$this->stream = create_stream($this->options[CURLOPT_POSTFIELDS]);
@@ -173,7 +172,7 @@ protected function getCurlError(int $status, $resource): Response
173172
'instance' => \curl_getinfo($resource, CURLINFO_EFFECTIVE_URL),
174173
'type' => 'https://httpstatuses.com/' . $status,
175174
'status' => $status,
176-
]), $status, ['Content-type' => 'application/problem+json']);
175+
]), $status, ['Content-Type' => 'application/problem+json']);
177176
}
178177

179178
/**

Client/PhpClient.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ public function read(): Response
6161
if ($this->hasError($resource)) {
6262
return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY);
6363
}
64-
$this->extractFromResponseHeaders($resource, $headers, $statusCode);
64+
$this->extractFromResponseHeaders($resource, $statusCode, $headers);
6565
return new ServerResponse(\stream_get_contents($resource), $statusCode, $headers);
6666
} catch (\ValueError $e) {
6767
return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY, $e->getMessage());
6868
} catch (\Throwable $e) {
69-
return $this->getPhpError(
70-
$e->getCode() ?: HttpStatus::INTERNAL_SERVER_ERROR,
71-
$e->getMessage());
69+
return $this->getPhpError(HttpStatus::INTERNAL_SERVER_ERROR, $e->getMessage());
7270
} finally {
7371
if (\is_resource($resource)) {
7472
\fclose($resource);
@@ -137,7 +135,7 @@ protected function prepareRequestBody(): void
137135
if (0 === $this->encoding) {
138136
$this->options['content'] = $this->stream->getContents();
139137
} elseif ($content = \json_decode($this->stream->getContents() ?: '[]', true)) {
140-
$this->normalizeHeader('Content-type', self::X_WWW_FORM_URLENCODED, true);
138+
$this->normalizeHeader('Content-Type', self::X_WWW_FORM_URLENCODED, true);
141139
$this->options['content'] = \http_build_query($content, null, '&', $this->encoding);
142140
}
143141
$this->stream = create_stream($this->options['content']);
@@ -159,20 +157,18 @@ protected function prepareOptions(): void
159157
* Extracts the headers and status code from the response.
160158
*
161159
* @param resource $response The resource from fopen()
162-
* @param array $headers Parsed response headers
163160
* @param int $statusCode Response status code
161+
* @param array $headers Parsed response headers
164162
*/
165-
protected function extractFromResponseHeaders($response, &$headers, &$statusCode): void
163+
protected function extractFromResponseHeaders($response, &$statusCode, &$headers): void
166164
{
167165
try {
168166
$meta = \stream_get_meta_data($response)['wrapper_data'] ?? [];
169167
/* HTTP status may not always be the first header in the response headers,
170168
* for example, if the stream follows one or multiple redirects, the last
171169
* status line is what is expected here.
172170
*/
173-
$statusCode = \array_filter($meta, function(string $header) {
174-
return \str_starts_with($header, 'HTTP/');
175-
});
171+
$statusCode = \array_filter($meta, fn(string $header) => \str_starts_with($header, 'HTTP/'));
176172
$statusCode = \array_pop($statusCode) ?: 'HTTP/1.1 200 OK';
177173
$statusCode = (int)(\explode(' ', $statusCode)[1] ?? HttpStatus::OK);
178174
foreach ($meta as $header) {

ClientRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ protected function getPhpError(int $status, ?string $message = null): Response
189189
'instance' => (string)$this->getUri(),
190190
'type' => 'https://httpstatuses.com/' . $status,
191191
'status' => $status,
192-
]), $status, ['Content-type' => 'application/problem+json']);
192+
]), $status, ['Content-Type' => 'application/problem+json']);
193193
}
194194
}

0 commit comments

Comments
 (0)