summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Piasecki <jakub@nymedia.no>2016-03-08 14:42:16 +0100
committerJakub Piasecki <jakub@nymedia.no>2016-03-08 14:42:16 +0100
commitb91a2f6d0ddc81442c7ea51ae54e03d8c31f373f (patch)
tree3e2bafb67f59d63ecdc8391f3e38845266b54e26
parente3f286a4d0ca70050136eee8482b6ab4c5032d3e (diff)
downloadphp-sparkpost-b91a2f6d0ddc81442c7ea51ae54e03d8c31f373f.zip
php-sparkpost-b91a2f6d0ddc81442c7ea51ae54e03d8c31f373f.tar.gz
php-sparkpost-b91a2f6d0ddc81442c7ea51ae54e03d8c31f373f.tar.bz2
Improve APIResponseException class
-rw-r--r--lib/SparkPost/APIResource.php20
-rw-r--r--lib/SparkPost/APIResponseException.php50
2 files changed, 62 insertions, 8 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index d02b91e..20e7b19 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -192,11 +192,19 @@ class APIResource {
// 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 );
+ }
+ elseif ($statusCode === 404) {
+ throw new APIResponseException('The specified resource does not exist', 404);
+ }
+ else {
+ $response = json_decode($response->getBody(), true);
+ throw new APIResponseException(
+ 'Received bad response from ' . ucfirst($this->endpoint),
+ $statusCode,
+ isset($response['errors'][0]['message']) ? $response['errors'][0]['message'] : "",
+ isset($response['errors'][0]['code']) ? $response['errors'][0]['code'] : 0,
+ isset($response['errors'][0]['description']) ? $response['errors'][0]['description'] : ""
+ );
}
}
@@ -208,7 +216,7 @@ class APIResource {
throw $exception;
}
- throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
+ throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage(), $exception->getCode());
}
}
diff --git a/lib/SparkPost/APIResponseException.php b/lib/SparkPost/APIResponseException.php
index cc0842c..b7e3896 100644
--- a/lib/SparkPost/APIResponseException.php
+++ b/lib/SparkPost/APIResponseException.php
@@ -3,7 +3,53 @@
namespace SparkPost;
class APIResponseException extends \Exception {
+ /**
+ * @var string
+ */
+ protected $apiMessage;
-}
+ /**
+ * @var int
+ */
+ protected $apiCode;
+
+ /**
+ * @var string
+ */
+ protected $apiMessageDescription;
+
+ /**
+ * Construct the exception.
+ */
+ public function __construct($message = "", $code = 0, $apiMessage = "", $apiCode = 0, $apiMessageDescription = "") {
+ $this->apiMessage = $apiMessage;
+ $this->apiCode = $apiCode;
+ $this->apiMessageDescription = $apiMessageDescription;
+ parent::__construct($message, $code);
+ }
+
+ /**
+ * Gets the Exception message
+ * @return string the Exception message as a string.
+ */
+ public function getAPIMessage() {
+ return $this->apiMessage;
+ }
-?>
+ /**
+ * Gets the API Exception code.
+ * @return int the exception code as integer.
+ */
+ public function getAPICode() {
+ return $this->apiCode;
+ }
+
+ /**
+ * Gets the Exception message
+ * @return string the Exception message as a string.
+ */
+ public function getAPIMessageDescription() {
+ return $this->apiMessageDescription;
+ }
+
+}