Skip to content

Commit

Permalink
Return extended exception data
Browse files Browse the repository at this point in the history
* Update examples to show available extra exception methods
* Resolves #83

* Update 403s to pass back extended exception data

Fixes #83
  • Loading branch information
richleland committed May 4, 2016
1 parent 48fcc7b commit d96d825
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 10 deletions.
4 changes: 3 additions & 1 deletion examples/transmission/delete_transmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
$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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/get_all_transmissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
$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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/get_transmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
$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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/rfc822.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
]);
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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/send_transmission_all_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/simple_send.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
]);
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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/stored_recipients_inline_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

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";
}
?>
4 changes: 3 additions & 1 deletion examples/transmission/stored_template_send.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
]);
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";
}
?>
11 changes: 9 additions & 2 deletions lib/SparkPost/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function delete( $resourcePath=null, Array $query=[] ) {
* 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) {
Expand Down Expand Up @@ -202,7 +202,14 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
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);
Expand Down
6 changes: 6 additions & 0 deletions test/unit/APIResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,18 @@ public function testAdapter404Exception() {
}

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

0 comments on commit d96d825

Please sign in to comment.