summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Leland <rich@richleland.com>2016-05-03 21:12:27 -0400
committerRichard Leland <rich@richleland.com>2016-05-03 21:12:27 -0400
commitd96d825aa138af92628ec7311c05e9659917ba79 (patch)
treec14aab1ebf1d7a50f9b72dce78679e54dd4942e0
parent48fcc7b0d1eb204a0a49c50f85dde16fbcb5c6b5 (diff)
downloadphp-sparkpost-d96d825aa138af92628ec7311c05e9659917ba79.zip
php-sparkpost-d96d825aa138af92628ec7311c05e9659917ba79.tar.gz
php-sparkpost-d96d825aa138af92628ec7311c05e9659917ba79.tar.bz2
Return extended exception data
* Update examples to show available extra exception methods * Resolves #83 * Update 403s to pass back extended exception data Fixes #83
-rw-r--r--examples/transmission/delete_transmission.php4
-rw-r--r--examples/transmission/get_all_transmissions.php4
-rw-r--r--examples/transmission/get_transmission.php4
-rw-r--r--examples/transmission/rfc822.php4
-rw-r--r--examples/transmission/send_transmission_all_fields.php4
-rw-r--r--examples/transmission/simple_send.php4
-rw-r--r--examples/transmission/stored_recipients_inline_content.php4
-rw-r--r--examples/transmission/stored_template_send.php4
-rw-r--r--lib/SparkPost/APIResource.php11
-rw-r--r--test/unit/APIResourceTest.php6
10 files changed, 39 insertions, 10 deletions
diff --git a/examples/transmission/delete_transmission.php b/examples/transmission/delete_transmission.php
index de116eb..9459066 100644
--- a/examples/transmission/delete_transmission.php
+++ b/examples/transmission/delete_transmission.php
@@ -17,6 +17,8 @@ try {
$results = $sparky->transmission->delete('transmission-id');
echo 'Transmission deleted!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/get_all_transmissions.php b/examples/transmission/get_all_transmissions.php
index d4d92c0..a465056 100644
--- a/examples/transmission/get_all_transmissions.php
+++ b/examples/transmission/get_all_transmissions.php
@@ -17,6 +17,8 @@ try {
$results = $sparky->transmission->all();
echo 'Congrats! You got a list of all your transmissions from SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/get_transmission.php b/examples/transmission/get_transmission.php
index 974c064..879e9c3 100644
--- a/examples/transmission/get_transmission.php
+++ b/examples/transmission/get_transmission.php
@@ -17,6 +17,8 @@ try {
$results = $sparky->transmission->find('Your Transmission ID');
echo 'Congrats! You retrieved your transmission from SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/rfc822.php b/examples/transmission/rfc822.php
index c27fafb..5ec5aef 100644
--- a/examples/transmission/rfc822.php
+++ b/examples/transmission/rfc822.php
@@ -26,6 +26,8 @@ try {
]);
echo 'Congrats! You sent an email using SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/send_transmission_all_fields.php b/examples/transmission/send_transmission_all_fields.php
index 51b6a89..9db99d2 100644
--- a/examples/transmission/send_transmission_all_fields.php
+++ b/examples/transmission/send_transmission_all_fields.php
@@ -61,6 +61,8 @@ try{
echo 'Congrats! You sent an email using SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/simple_send.php b/examples/transmission/simple_send.php
index 08bef9a..5d2d6e3 100644
--- a/examples/transmission/simple_send.php
+++ b/examples/transmission/simple_send.php
@@ -32,6 +32,8 @@ try {
]);
echo 'Congrats! You sent an email using SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/stored_recipients_inline_content.php b/examples/transmission/stored_recipients_inline_content.php
index b31167c..8b16456 100644
--- a/examples/transmission/stored_recipients_inline_content.php
+++ b/examples/transmission/stored_recipients_inline_content.php
@@ -29,6 +29,8 @@ try {
echo 'Congrats! You sent an email using SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/examples/transmission/stored_template_send.php b/examples/transmission/stored_template_send.php
index de0c01c..4d4adfe 100644
--- a/examples/transmission/stored_template_send.php
+++ b/examples/transmission/stored_template_send.php
@@ -30,6 +30,8 @@ try {
]);
echo 'Congrats! You sent an email using SparkPost!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getAPIMessage() . "\n";
+ echo $exception->getAPICode() . "\n";
+ echo $exception->getAPIDescription() . "\n";
}
?>
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index 10305f2..f593038 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -131,7 +131,7 @@ class APIResource {
* assembles a URL for a request
* @param string $resourcePath path after the initial endpoint
* @param array $options array with an optional value of query with values to build a querystring from. Any
- * query elements that are themselves arrays will be imploded into a comma separated list.
+ * query elements that are themselves arrays will be imploded into a comma separated list.
* @return string the assembled URL
*/
private function buildUrl($resourcePath, $options) {
@@ -202,7 +202,14 @@ class APIResource {
return json_decode($response->getBody()->getContents(), true);
}
elseif ($statusCode === 403) {
- throw new APIResponseException('Request forbidden. Does this API Key have the correct SparkPost permissions?');
+ $response = json_decode($response->getBody(), true);
+ throw new APIResponseException(
+ 'Request forbidden',
+ $statusCode,
+ isset($response['errors'][0]['message']) ? $response['errors'][0]['message'] : "Request forbidden",
+ isset($response['errors'][0]['code']) ? $response['errors'][0]['code'] : 1100,
+ isset($response['errors'][0]['description']) ? $response['errors'][0]['description'] : "Does this API Key have the correct permissions?"
+ );
}
elseif ($statusCode === 404) {
throw new APIResponseException('The specified resource does not exist', 404);
diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php
index c2b0c2a..8552815 100644
--- a/test/unit/APIResourceTest.php
+++ b/test/unit/APIResourceTest.php
@@ -131,12 +131,18 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
}
public function testAdapter403Exception() {
+ $testBody = [ 'errors' => [
+ [
+ 'message' => 'Forbidden.'
+ ]
+ ]];
try {
$responseMock = Mockery::mock();
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
once()->
andReturn($responseMock);
$responseMock->shouldReceive('getStatusCode')->andReturn(403);
+ $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody));
$this->resource->get('test');
}