Skip to content

Commit 2bac548

Browse files
committed
Merge pull request #51 from zaporylie/feat/customize-api-response-exception
Improve APIResponseException class
2 parents e8e11de + 487245e commit 2bac548

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

lib/SparkPost/APIResource.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,19 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
192192
// Handle 4XX responses, 5XX responses will throw an HttpAdapterException
193193
if ($statusCode < 400) {
194194
return json_decode($response->getBody()->getContents(), true);
195-
} else {
196-
if ($statusCode === 404) {
197-
throw new APIResponseException('The specified resource does not exist', 404);
198-
}
199-
throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode );
195+
}
196+
elseif ($statusCode === 404) {
197+
throw new APIResponseException('The specified resource does not exist', 404);
198+
}
199+
else {
200+
$response = json_decode($response->getBody(), true);
201+
throw new APIResponseException(
202+
'Received bad response from ' . ucfirst($this->endpoint),
203+
$statusCode,
204+
isset($response['errors'][0]['message']) ? $response['errors'][0]['message'] : "",
205+
isset($response['errors'][0]['code']) ? $response['errors'][0]['code'] : 0,
206+
isset($response['errors'][0]['description']) ? $response['errors'][0]['description'] : ""
207+
);
200208
}
201209
}
202210

@@ -208,7 +216,7 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
208216
throw $exception;
209217
}
210218

211-
throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
219+
throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage(), $exception->getCode());
212220
}
213221
}
214222

lib/SparkPost/APIResponseException.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,53 @@
33
namespace SparkPost;
44

55
class APIResponseException extends \Exception {
6+
/**
7+
* @var string
8+
*/
9+
protected $apiMessage;
610

7-
}
11+
/**
12+
* @var int
13+
*/
14+
protected $apiCode;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $apiDescription;
20+
21+
/**
22+
* Construct the exception.
23+
*/
24+
public function __construct($message = "", $code = 0, $apiMessage = "", $apiCode = 0, $apiDescription = "") {
25+
$this->apiMessage = $apiMessage;
26+
$this->apiCode = $apiCode;
27+
$this->apiDescription = $apiDescription;
28+
parent::__construct($message, $code);
29+
}
30+
31+
/**
32+
* Gets the Exception message
33+
* @return string the Exception message as a string.
34+
*/
35+
public function getAPIMessage() {
36+
return $this->apiMessage;
37+
}
838

9-
?>
39+
/**
40+
* Gets the API Exception code.
41+
* @return int the exception code as integer.
42+
*/
43+
public function getAPICode() {
44+
return $this->apiCode;
45+
}
46+
47+
/**
48+
* Gets the Exception description
49+
* @return string the Exception description as a string.
50+
*/
51+
public function getAPIDescription() {
52+
return $this->apiDescription;
53+
}
54+
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace SparkPost\Test;
3+
use SparkPost\APIResponseException;
4+
5+
class APIResourceExceptionTest extends \PHPUnit_Framework_TestCase {
6+
7+
private $message;
8+
private $code;
9+
private $description;
10+
private $exception;
11+
12+
/**
13+
* (non-PHPdoc)
14+
* @before
15+
* @see PHPUnit_Framework_TestCase::setUp()
16+
*/
17+
public function setUp() {
18+
$this->message = 'Test message';
19+
$this->code = 400;
20+
$this->description = 'Test description';
21+
$this->exception = new APIResponseException(NULL, 0, $this->message, $this->code, $this->description);
22+
}
23+
24+
public function testAPIMessage() {
25+
$this->assertEquals($this->message, $this->exception->getAPIMessage());
26+
}
27+
28+
public function testAPICode() {
29+
$this->assertEquals($this->code, $this->exception->getAPICode());
30+
}
31+
32+
public function testAPIDescription() {
33+
$this->assertEquals($this->description, $this->exception->getAPIDescription());
34+
}
35+
36+
}

test/unit/APIResourceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ public function testAdapter404Exception() {
113113

114114
public function testAdapter4XXException() {
115115
try {
116+
$testBody = ['errors'=>['my'=>'test']];
116117
$responseMock = Mockery::mock();
117118
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
118119
once()->
119120
andReturn($responseMock);
120121
$responseMock->shouldReceive('getStatusCode')->andReturn(400);
122+
$responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody));
121123

122124
$this->resource->get('test');
123125
}

0 commit comments

Comments
 (0)