Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passed curl errors along to the user #165

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 8 additions & 7 deletions src/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/MailChimpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}