Skip to content

Commit

Permalink
Merge pull request #13 from ebizmarts/Issue12
Browse files Browse the repository at this point in the history
add some extra data in Mailchimp_Error to debug and add the method ge…
  • Loading branch information
gonzaloebiz committed Aug 1, 2018
2 parents 13e6271 + 285972e commit 471e56b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ebizmarts/mailchimp-lib",
"type": "library",
"version": "3.0.23",
"version": "3.0.24",
"description": "API client library for the MailChimp",
"keywords": ["email", "api","mailchimp"],
"homepage": "https://github.com/ebizmarts/mailchimp-lib",
Expand Down
9 changes: 4 additions & 5 deletions src/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,10 @@ public function call($url,$params,$method=Mailchimp::GET)
$result = json_decode($response_body, true);

if(floor($info['http_code'] / 100) >= 4) {
if(array_key_exists('detail',$result)) {
throw new Mailchimp_Error($result['title'] . ' : ' . $result['detail']);
} else {
throw new Mailchimp_Error($result['title']);
}
$detail = array_key_exists('detail', $result) ? $result['detail'] : '';
$errors = array_key_exists('errors',$result) ? $result['errors'] : null;
$title = array_key_exists('title',$result) ? $result['title'] : '';
throw new Mailchimp_Error($this->_root . $url, $method, $params, $title, $detail, $errors);
}

return $result;
Expand Down
77 changes: 76 additions & 1 deletion src/Mailchimp/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,80 @@
* @date: 4/27/16 4:45 PM
* @file: Exceptions.php
*/
class Mailchimp_Error extends Exception{}
class Mailchimp_Error extends Exception
{
/**
* @var string
*/
protected $url;
/**
* @var string
*/
protected $title;
/**
* @var string
*/
protected $detail;
/**
* @var string
*/
protected $method;
/**
* @var array
*/
protected $errors;
/**
* @var string
*/
protected $params;

public function __construct($url,$method,$params,$title,$detail,$errors)
{
$titleComplete = $title . " for Api Call: " . $url;
parent::__construct($titleComplete . " - " . $detail);
$this->url = $url;
$this->title = $title;
$this->detail = $detail;
$this->method = $method;
$this->errors = $errors;
$this->params = $params;
}
public function getFriendlyMessage()
{
$friendlyMessage = $this->title . " for Api Call: [" . $this->url. "] using method [".$this->method."]\n";
$friendlyMessage .= "\tDetail: [".$this->detail."]\n";
if(is_array($this->errors)) {
$errorMessage = '';
foreach ($this->errors as $error) {
$field = array_key_exists('field', $error) ? $error['field'] : '';
$message = array_key_exists('message', $error) ? $error['message'] : '';
$line = "\t\t field [$field] : $message\n";
$errorMessage .= $line;
}
$friendlyMessage .= "\tErrors:\n".$errorMessage;
}
$friendlyMessage .= "\tParams:\n\t\t".$this->params;
return $friendlyMessage;
}
public function getUrl()
{
return $this->url;
}
public function getTitle()
{
return$this->title;
}
public function getDetail()
{
return $this->detail;
}
public function getMethod()
{
return $this->method;
}
public function getErrors()
{
return $this->errors;
}
}
class Mailchimp_HttpError extends Mailchimp_Error {}

0 comments on commit 471e56b

Please sign in to comment.