Skip to content

Commit dcc265b

Browse files
committed
error handling update
1 parent 494ae5f commit dcc265b

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

src/Exceptions/ClientError.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,49 @@ class ClientError extends ErrorException implements RequestExceptionInterface
1313
{
1414
protected RequestInterface $request;
1515

16-
protected ResponseInterface $response;
16+
protected ?ResponseInterface $response = null;
1717

18-
public function __construct(RequestInterface $request, ResponseInterface $response)
18+
public function __construct(RequestInterface $request, ?ResponseInterface $response = null)
1919
{
2020
$this->request = $request;
2121
$this->response = $response;
22+
parent::__construct($this->createMessage(), $response instanceof ResponseInterface ? $response->getStatusCode() : 0);
23+
}
24+
25+
protected function createMessage(): string
26+
{
27+
if (!$this->response instanceof ResponseInterface) {
28+
return sprintf(
29+
'Http request "%s %s" failed to get a response',
30+
$this->request->getMethod(),
31+
$this->request->getUri()
32+
);
33+
}
2234

23-
$code = $response->getStatusCode();
2435
$message = sprintf(
2536
'Http request "%s %s" returned %u response',
26-
$request->getMethod(),
27-
$request->getUri(),
28-
$code
37+
$this->request->getMethod(),
38+
$this->request->getUri(),
39+
$this->response->getStatusCode()
2940
);
3041

3142
if (strpos($this->response->getHeaderLine('Content-Type'), '/json') !== false) {
32-
$data = json_decode((string) $this->response->getBody(), true);
43+
$data = json_decode((string) $this->response->getBody(), true, 512, JSON_THROW_ON_ERROR);
3344
if (is_array($data) && (isset($data['title']) || isset($data['detail']))) {
3445
$separator = isset($data['title'], $data['detail']) ? "\n\n" : '';
3546
$message = ($data['title'] ?? '').$separator.($data['detail'] ?? '');
3647
}
3748
}
3849

39-
parent::__construct($message, $code);
50+
return $message;
4051
}
4152

4253
public function getRequest(): RequestInterface
4354
{
4455
return $this->request;
4556
}
4657

47-
public function getResponse(): ResponseInterface
58+
public function getResponse(): ?ResponseInterface
4859
{
4960
return $this->response;
5061
}

src/HttpClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected function buildResponse(StreamInterface $headers, StreamInterface $body
154154
return $this->parseHeaders($response, $headers);
155155
}
156156

157-
public function sendRequest(RequestInterface $request): ResponseInterface
157+
protected function prepareSession(RequestInterface $request): array
158158
{
159159
curl_setopt_array($this->session, $this->buildOptions($request));
160160

@@ -178,6 +178,13 @@ static function ($session, string $data) use ($body): int {
178178
}
179179
);
180180

181+
return [$headers, $body];
182+
}
183+
184+
public function sendRequest(RequestInterface $request): ResponseInterface
185+
{
186+
[$headers, $body] = $this->prepareSession($request);
187+
181188
$result = curl_exec($this->session);
182189
if ($this->debug) {
183190
$this->metrics = curl_getinfo($this->session);

0 commit comments

Comments
 (0)