Skip to content

Commit bf1930d

Browse files
committed
improved json error reporting
1 parent 1247dee commit bf1930d

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/MailChimpAPI.php

+37-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
/**
1414
* Class MailChimpAPI
1515
* @package MailChimp
16+
*
17+
* @method mixed get($resource, array $options = [])
18+
* @method mixed head($resource, array $options = [])
19+
* @method mixed put($resource, array $options = [])
20+
* @method mixed post($resource, array $options = [])
21+
* @method mixed patch($resource, array $options = [])
22+
* @method mixed delete($resource, array $options = [])
1623
*/
1724
class MailChimpAPI {
1825

@@ -147,28 +154,42 @@ private function makeRequest($resource, $arguments, $method) {
147154
try {
148155

149156
$options = $this->getOptions($method, $arguments);
150-
$response = $this->client->{$method}($this->endpoint . $resource, $options);
157+
$response = $this->client->{$method}(
158+
( $this->endpoint . $resource ),
159+
$options
160+
);
151161

152162
//plain old json_decode
153163
$data = json_decode( $response->getBody() );
154164

155165
if( $de = json_last_error() )
156-
throw new MailChimpAPIException("Could not decode API response... .");
166+
throw new MailChimpAPIException(
167+
"Could not decode API response... .
168+
JSON error #{$de}: " . json_last_error_msg()
169+
);
157170

158171
return $data;
159172

160173
} catch (ClientException $e) {
161-
throw new MailChimpAPIException($e->getResponse()->getBody(), $e->getResponse()->getStatusCode(), $e);
174+
throw new MailChimpAPIException(
175+
$e->getResponse()->getBody(),
176+
$e->getResponse()->getStatusCode(),
177+
$e
178+
);
162179

163180
} catch (RequestException $e) {
164181

165182
$response = $e->getResponse();
166183

167-
if ($response instanceof ResponseInterface) {
168-
throw new MailChimpAPIException($e->getResponse()->getBody(), $e->getResponse()->getStatusCode(), $e);
169-
}
184+
if ( $response instanceof ResponseInterface )
185+
throw new MailChimpAPIException(
186+
$e->getResponse()->getBody(),
187+
$e->getResponse()->getStatusCode(),
188+
$e
189+
);
170190

171-
throw new MailChimpAPIException($e->getMessage());
191+
192+
throw new MailChimpAPIException( $e->getMessage() );
172193

173194
}
174195

@@ -204,16 +225,18 @@ private function getOptions($method, $arguments) {
204225
*/
205226
public function __call($method, $arguments) {
206227

207-
if (count($arguments) < 1) {
208-
throw new InvalidArgumentException('Magic request methods require a URI and optional options array');
209-
}
228+
if ( count($arguments) < 1 )
229+
throw new InvalidArgumentException(
230+
'Magic request methods require a URI and optional options array'
231+
);
210232

211-
if ( ! in_array($method, $this->allowedMethods)) {
212-
throw new BadMethodCallException('Method "' . $method . '" is not supported.');
213-
}
233+
if ( ! in_array($method, $this->allowedMethods) )
234+
throw new BadMethodCallException(
235+
'Method "' . $method . '" is not supported.'
236+
);
214237

215238
$resource = $arguments[0];
216-
$options = isset($arguments[1]) ? $arguments[1] : [];
239+
$options = isset($arguments[1]) ? $arguments[1] : [];
217240

218241
return $this->request($resource, $options, $method);
219242

0 commit comments

Comments
 (0)