diff --git a/composer.json b/composer.json index 64468e2..9cbdd11 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Mailchimp.php b/src/Mailchimp.php index 893d050..3a566cf 100644 --- a/src/Mailchimp.php +++ b/src/Mailchimp.php @@ -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; diff --git a/src/Mailchimp/Exceptions.php b/src/Mailchimp/Exceptions.php index 12c8822..04c437b 100644 --- a/src/Mailchimp/Exceptions.php +++ b/src/Mailchimp/Exceptions.php @@ -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 {}