Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/SparkPost/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,19 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
// 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'] : ""
);
}
}

Expand All @@ -208,7 +216,7 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
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());
}
}

Expand Down
50 changes: 48 additions & 2 deletions lib/SparkPost/APIResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
36 changes: 36 additions & 0 deletions test/unit/APIResourceExceptionTest.php
Original file line number Diff line number Diff line change
@@ -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());
}

}
2 changes: 2 additions & 0 deletions test/unit/APIResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ public function testAdapter404Exception() {

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');
}
Expand Down