From cc0b9f73c51f87157941ca57f33217973d27154b Mon Sep 17 00:00:00 2001 From: Mark McEver Date: Mon, 16 Jan 2017 10:03:32 -0600 Subject: [PATCH 1/3] Passed curl errors along to the user (they were previously getting swallowed) --- src/MailChimp.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index c399cdb..3450450 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -309,7 +309,9 @@ private function determineSuccess($response, $formattedResponse) return false; } - $this->last_error = 'Unknown error, call getLastResponse() to find out what happened.'; + // The original error is appended so that curl errors are communicated to the user. + // An example of one such curl error is #60: SSL certificate problem: unable to get local issuer certificate + $this->last_error = 'Unknown error, call getLastResponse() for more details: ' . $this->last_error; return false; } From 9cf088984706044ef3271649af7a7e785afa5647 Mon Sep 17 00:00:00 2001 From: Mark McEver Date: Mon, 6 Mar 2017 20:11:22 -0600 Subject: [PATCH 2/3] Revert "Passed curl errors along to the user (they were previously getting swallowed)" This reverts commit cc0b9f73c51f87157941ca57f33217973d27154b. --- src/MailChimp.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index 3450450..c399cdb 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -309,9 +309,7 @@ private function determineSuccess($response, $formattedResponse) return false; } - // The original error is appended so that curl errors are communicated to the user. - // An example of one such curl error is #60: SSL certificate problem: unable to get local issuer certificate - $this->last_error = 'Unknown error, call getLastResponse() for more details: ' . $this->last_error; + $this->last_error = 'Unknown error, call getLastResponse() to find out what happened.'; return false; } From a1a5da27823842555d3ea31a40b6298149c51a1b Mon Sep 17 00:00:00 2001 From: Mark McEver Date: Mon, 6 Mar 2017 20:56:54 -0600 Subject: [PATCH 3/3] Passed curl errors along to the user (they were previously getting swallowed) This solution takes into account robertark's suggestion of avoiding calling determineSuccess() all together if a curl error is detected. --- src/MailChimp.php | 15 ++++++++------- tests/MailChimpTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index da9bf8f..ba1c331 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -249,7 +249,8 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se $response['headers'] = curl_getinfo($ch); if ($responseContent === false) { - $this->last_error = curl_error($ch); + $this->last_error = "Curl Error: " . curl_error($ch); + $formattedResponse = false; } else { $headerSize = $response['headers']['header_size']; @@ -259,14 +260,16 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se if (isset($response['headers']['request_header'])) { $this->last_request['headers'] = $response['headers']['request_header']; } + + $formattedResponse = $this->formatResponse($response); + + $this->determineSuccess($response, $formattedResponse, $timeout); } + $this->last_response = $response; + curl_close($ch); - $formattedResponse = $this->formatResponse($response); - - $this->determineSuccess($response, $formattedResponse, $timeout); - return $formattedResponse; } @@ -360,8 +363,6 @@ private function attachRequestPayload(&$ch, $data) */ private function formatResponse($response) { - $this->last_response = $response; - if (!empty($response['body'])) { return json_decode($response['body'], true); } diff --git a/tests/MailChimpTest.php b/tests/MailChimpTest.php index 6a62269..94b7e9f 100644 --- a/tests/MailChimpTest.php +++ b/tests/MailChimpTest.php @@ -101,4 +101,17 @@ public function testRequestTimeout() $error = $MailChimp->getLastError(); $this->assertRegExp( '/Request timed out after 1.\d+ seconds/', $error ); } + + public function testCurlErrorPassthrough() + { + $badDataCenter = 'us999999999'; + + $MailChimp = new MailChimp("abc123-$badDataCenter"); + + $MailChimp->get('lists'); + + // Ensure that the curl error is not overwritten and makes it back to the user. + // If this assertion passes, other curl errors should make it back to the user as well. + $this->assertEquals("Curl Error: Couldn't resolve host '$badDataCenter.api.mailchimp.com'", $MailChimp->getLastError()); + } }