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()); + } }