Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Handle curl error 0 and show its error message #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/KlaviyoAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class KlaviyoAPI
const ERROR_INVALID_API_KEY = 'Invalid API Key.';
const ERROR_RESOURCE_DOES_NOT_EXIST = 'The requested resource does not exist.';
const ERROR_NON_200_STATUS = 'Request Failed with HTTP Status Code: %s';
const ERROR_CURL_ERROR = 'Request Failed with CURL error: %s';

/**
* Request options
Expand Down Expand Up @@ -174,13 +175,19 @@ private function request($method, $path, $options, $isPublic = false, $isV1 = fa
$statusCode = curl_getinfo($curl, $phpVersionHttpCode);
curl_close($curl);

return $this->handleResponse($response, $statusCode, $isPublic);
if ( $statusCode == 0 && curl_errno($curl)) {
$curlError = curl_error($curl);
} else {
$curlError = null;
}

return $this->handleResponse($response, $statusCode, $curlError, $isPublic);
}

/**
* Handle response from API call
*/
private function handleResponse($response, $statusCode, $isPublic)
private function handleResponse($response, $statusCode, $curlError, $isPublic)
{
$decoded_response = $this->decodeJsonResponse($response);
if ($statusCode == 403) {
Expand All @@ -190,7 +197,14 @@ private function handleResponse($response, $statusCode, $isPublic)
$this->returnRateLimit($decoded_response)
);
} else if ($statusCode < 200 || $statusCode >= 300) {
throw new KlaviyoApiException(isset($decoded_response['detail']) ? $decoded_response['detail'] : sprintf(self::ERROR_NON_200_STATUS, $statusCode), $statusCode);
if ($curlError) {
$errorMsg = sprintf(self::ERROR_CURL_ERROR, $curlError);
} elseif (isset($decoded_response['detail'])) {
$errorMsg = $decoded_response['detail'];
} else {
$errorMsg = sprintf(self::ERROR_NON_200_STATUS, $statusCode);
}
throw new KlaviyoApiException($errorMsg, $statusCode);
}

if ($isPublic) {
Expand Down