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

Errors array #236

Open
wants to merge 5 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ if ($MailChimp->success()) {
print_r($result);
} else {
echo $MailChimp->getLastError();
if(!empty($MailChimp->getErrorsArray())){
echo "\nErrors array messages:";
foreach($MailChimp->getErrorsArray() as $error){
echo "\n".$error['field'].":".$error['message'];
}
}

}
```

Expand Down
19 changes: 18 additions & 1 deletion src/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* This wrapper: https://github.com/drewm/mailchimp-api
*
* @author Drew McLellan <[email protected]>
* @version 2.5
* @author Matteo Lazzarin <[email protected]>
* @version 2.6
*/
class MailChimp
{
Expand All @@ -27,6 +28,7 @@ class MailChimp
private $last_error = '';
private $last_response = array();
private $last_request = array();
private $errors_array = array();

/**
* Create a new instance
Expand Down Expand Up @@ -110,6 +112,17 @@ public function getLastError()
{
return $this->last_error ?: false;
}

/**
* Get errors array, in case of multiple errors got back from body response.
* Generically, this errors are thrown from wrong inserted MERGE_FIELDS value, giving a 400 status code
*
* @return array the errors array
*/
public function getErrorsArray()
{
return $this->errors_array;
}

/**
* Get an array containing the HTTP headers and the body of the API response.
Expand Down Expand Up @@ -451,6 +464,10 @@ private function determineSuccess($response, $formattedResponse, $timeout)
$this->request_successful = true;
return true;
}

if (isset($formattedResponse['errors'])){
$this->errors_array = $formattedResponse['errors'];
}

if (isset($formattedResponse['detail'])) {
$this->last_error = sprintf('%d: %s', $formattedResponse['status'], $formattedResponse['detail']);
Expand Down