Skip to content

Commit b10293f

Browse files
authored
Merge pull request #51 from vbcodeplicity/feature/API-5760-Expand-the-connect-timeout-exception
Expand curl_error exception handling with more details
2 parents 92187c7 + 0b3c9c4 commit b10293f

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/Engine/Elasticsearch/ElasticsearchClient.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ private function submitCurlRequest()
248248
$error = curl_error($handle);
249249

250250
if ($error) {
251-
throw new ClientException(sprintf("Submit curl request failed. Error: %s", $error));
251+
$errorCode = "Curl error number - ". curl_errno($handle);
252+
$this->throwCurlException($errorCode, $error, $this->url, $this->body, null);
252253
}
253254

254255
$info = curl_getinfo($handle);
@@ -264,15 +265,7 @@ private function submitCurlRequest()
264265
$this->profiler->end($uniqueId);
265266

266267
if (isset($info['http_code']) && (int) $info['http_code'] >= 400 && (int) $info['http_code'] <= 599) {
267-
throw new ClientException(
268-
sprintf(
269-
"Unexpected response code:%s from ES has been returned on submit. More info: %s. Body: %s. Response: %s",
270-
$info['http_code'],
271-
json_encode($info),
272-
json_encode($this->body),
273-
is_array($response) ? json_encode($response) : $response
274-
)
275-
);
268+
$this->throwCurlException($info['http_code'], $info, $this->url, $this->body, $response);
276269
}
277270

278271
return $this;
@@ -292,4 +285,18 @@ public function getUrl()
292285
{
293286
return $this->url;
294287
}
288+
289+
private function throwCurlException($code, $message, $url, $body, $response)
290+
{
291+
throw new ClientException(
292+
sprintf(
293+
"Unexpected response code:%s from ES has been returned on submit. More info: %s. Url: %s. Body: %s. Response: %s",
294+
$code,
295+
json_encode($message),
296+
json_encode($url),
297+
json_encode($body),
298+
is_array($response) ? json_encode($response) : $response
299+
)
300+
);
301+
}
295302
}

tests/unit/src/Engine/Elasticsearch/ElasticsearchClientTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
use G4\DataMapper\Engine\Elasticsearch\ElasticsearchClient;
5+
use G4\DataMapper\Exception\ClientException;
56
use G4\ValueObject\Url;
67

78
class ElasticsearchClientTest extends PHPUnit_Framework_TestCase
@@ -42,6 +43,14 @@ public function testGetUrl()
4243
$this->assertEquals($this->urlMock, $this->elasticsearchClient->getUrl());
4344
}
4445

46+
public function testCurlError()
47+
{
48+
$this->expectException(ClientException::class);
49+
$this->expectExceptionMessage('Unexpected response code:Curl error number - 6 from ES has been returned on submit. More info: "Could not resolve host: nothing". Url: {}. Body: null. Response: ');
50+
$elasticsearchClient = new ElasticsearchClient(new Url('http://nothing'), null, 5);
51+
$elasticsearchClient->execute();
52+
}
53+
4554
protected function setUp()
4655
{
4756
$this->urlMock = $this->getMockBuilder(Url::class)

0 commit comments

Comments
 (0)