From 24cee46a4c33f49abd433c73d2a58cc5d9baf35c Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Mon, 22 Jan 2018 18:32:04 +0100 Subject: [PATCH] fix curl error handler --- lib/Segment/Consumer/LibCurl.php | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/Segment/Consumer/LibCurl.php b/lib/Segment/Consumer/LibCurl.php index 6bded9a..0da33a6 100644 --- a/lib/Segment/Consumer/LibCurl.php +++ b/lib/Segment/Consumer/LibCurl.php @@ -78,24 +78,32 @@ public function flushBatch($messages) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // retry failed requests just once to diminish impact on performance - $httpResponse = $this->executePost($ch); + $responseContent = curl_exec($ch); + + $err = curl_error($ch); + if ($err) { + $this->handleError(curl_errno($ch), $err); + return; + } + + $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //close connection curl_close($ch); $elapsed_time = microtime(true) - $start_time; - if (200 != $httpResponse) { + if (200 != $responseCode) { // log error - $this->handleError($ch, $httpResponse); + $this->handleError($responseCode, $responseContent); - if (($httpResponse >= 500 && $httpResponse <= 600) || 429 == $httpResponse) { + if (($responseCode >= 500 && $responseCode <= 600) || 429 == $responseCode) { // If status code is greater than 500 and less than 600, it indicates server error // Error code 429 indicates rate limited. // Retry uploading in these cases. usleep($backoff * 1000); $backoff *= 2; - } elseif ($httpResponse >= 400) { + } elseif ($responseCode >= 400) { break; } } else { @@ -103,13 +111,6 @@ public function flushBatch($messages) { } } - return $httpResponse; - } - - public function executePost($ch) { - curl_exec($ch); - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - - return $httpCode; + return $responseCode; } }