summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Leland <rich@richleland.com>2016-03-13 20:43:46 -0400
committerRichard Leland <rich@richleland.com>2016-03-13 20:43:46 -0400
commit2bac54834fcc991383f89cf4ad24f710f0694089 (patch)
tree45d0e6e3592bb41dfa261ff87ba3ef882314dd15
parente8e11deed222255c577c35818b7c364b20aa1e4e (diff)
parent487245e0911b2c0dbf63ee81a20061419d1f177e (diff)
downloadphp-sparkpost-2bac54834fcc991383f89cf4ad24f710f0694089.zip
php-sparkpost-2bac54834fcc991383f89cf4ad24f710f0694089.tar.gz
php-sparkpost-2bac54834fcc991383f89cf4ad24f710f0694089.tar.bz2
Merge pull request #51 from zaporylie/feat/customize-api-response-exception
Improve APIResponseException class
-rw-r--r--lib/SparkPost/APIResource.php20
-rw-r--r--lib/SparkPost/APIResponseException.php50
-rw-r--r--test/unit/APIResourceExceptionTest.php36
-rw-r--r--test/unit/APIResourceTest.php2
4 files changed, 100 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..bc0e782 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 $apiDescription;
+
+ /**
+ * Construct the exception.
+ */
+ public function __construct($message = "", $code = 0, $apiMessage = "", $apiCode = 0, $apiDescription = "") {
+ $this->apiMessage = $apiMessage;
+ $this->apiCode = $apiCode;
+ $this->apiDescription = $apiDescription;
+ 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 description
+ * @return string the Exception description as a string.
+ */
+ public function getAPIDescription() {
+ return $this->apiDescription;
+ }
+
+}
diff --git a/test/unit/APIResourceExceptionTest.php b/test/unit/APIResourceExceptionTest.php
new file mode 100644
index 0000000..5bb4758
--- /dev/null
+++ b/test/unit/APIResourceExceptionTest.php
@@ -0,0 +1,36 @@
+<?php
+namespace SparkPost\Test;
+use SparkPost\APIResponseException;
+
+class APIResourceExceptionTest extends \PHPUnit_Framework_TestCase {
+
+ private $message;
+ private $code;
+ private $description;
+ private $exception;
+
+ /**
+ * (non-PHPdoc)
+ * @before
+ * @see PHPUnit_Framework_TestCase::setUp()
+ */
+ public function setUp() {
+ $this->message = 'Test message';
+ $this->code = 400;
+ $this->description = 'Test description';
+ $this->exception = new APIResponseException(NULL, 0, $this->message, $this->code, $this->description);
+ }
+
+ public function testAPIMessage() {
+ $this->assertEquals($this->message, $this->exception->getAPIMessage());
+ }
+
+ public function testAPICode() {
+ $this->assertEquals($this->code, $this->exception->getAPICode());
+ }
+
+ public function testAPIDescription() {
+ $this->assertEquals($this->description, $this->exception->getAPIDescription());
+ }
+
+}
diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php
index 18b0d3f..0baef41 100644
--- a/test/unit/APIResourceTest.php
+++ b/test/unit/APIResourceTest.php
@@ -113,11 +113,13 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
public function testAdapter4XXException() {
try {
+ $testBody = ['errors'=>['my'=>'test']];
$responseMock = Mockery::mock();
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
once()->
andReturn($responseMock);
$responseMock->shouldReceive('getStatusCode')->andReturn(400);
+ $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody));
$this->resource->get('test');
}