summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost
diff options
context:
space:
mode:
authorRichard Leland <rich@richleland.com>2016-02-25 16:38:28 -0500
committerRichard Leland <rich@richleland.com>2016-02-25 16:38:28 -0500
commit3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8 (patch)
tree79dc5fb54be024a023e02340e12be34614cb12fe /lib/SparkPost
parent7ff00e6104ac19d9a2fe20a8a03df469cb6910f6 (diff)
parentdad35ca70de8bbcdbd3a53e3deb761f961221dea (diff)
downloadphp-sparkpost-3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8.zip
php-sparkpost-3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8.tar.gz
php-sparkpost-3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8.tar.bz2
Merge pull request #39 from SparkPost/issue38
version bump, error response handling
Diffstat (limited to 'lib/SparkPost')
-rw-r--r--lib/SparkPost/APIResource.php31
-rw-r--r--lib/SparkPost/APIResponseException.php9
2 files changed, 28 insertions, 12 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index a2cd5a6..563a56e 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -3,6 +3,8 @@ namespace SparkPost;
use Ivory\HttpAdapter\HttpAdapterException;
use SparkPost\SparkPost;
+
+
/**
* @desc SDK interface for managing SparkPost API endpoints
*/
@@ -186,24 +188,29 @@ class APIResource {
//make request
try {
$response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
- return json_decode($response->getBody()->getContents(), true);
- }
- /*
- * Handles 4XX responses
- */
- catch (HttpAdapterException $exception) {
- $response = $exception->getResponse();
+
$statusCode = $response->getStatusCode();
- if($statusCode === 404) {
- throw new \Exception('The specified resource does not exist', 404);
+
+ // Handle 4XX responses, 5XX responses will throw an HttpAdapterException
+ if ($statusCode < 400) {
+ return json_decode($response->getBody()->getContents(), true);
+ } else {
+ if ($statusCode === 404) {
+ throw new APIResponseException('The specified resource does not exist', 404);
+ }
+ throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode );
}
- throw new \Exception('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode );
}
+
/*
- * Handles 5XX Errors, Configuration Errors, and a catch all for other errors
+ * Configuration Errors, and a catch all for other errors
*/
catch (\Exception $exception) {
- throw new \Exception('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage());
+ if($exception instanceof APIResponseException) {
+ throw $exception;
+ }
+
+ throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
}
}
diff --git a/lib/SparkPost/APIResponseException.php b/lib/SparkPost/APIResponseException.php
new file mode 100644
index 0000000..cc0842c
--- /dev/null
+++ b/lib/SparkPost/APIResponseException.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace SparkPost;
+
+class APIResponseException extends \Exception {
+
+}
+
+?>