Skip to content

Commit d96d825

Browse files
committed
Return extended exception data
* Update examples to show available extra exception methods * Resolves #83 * Update 403s to pass back extended exception data Fixes #83
1 parent 48fcc7b commit d96d825

10 files changed

+39
-10
lines changed

examples/transmission/delete_transmission.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
$results = $sparky->transmission->delete('transmission-id');
1818
echo 'Transmission deleted!';
1919
} catch (\Exception $exception) {
20-
echo $exception->getMessage();
20+
echo $exception->getAPIMessage() . "\n";
21+
echo $exception->getAPICode() . "\n";
22+
echo $exception->getAPIDescription() . "\n";
2123
}
2224
?>

examples/transmission/get_all_transmissions.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
$results = $sparky->transmission->all();
1818
echo 'Congrats! You got a list of all your transmissions from SparkPost!';
1919
} catch (\Exception $exception) {
20-
echo $exception->getMessage();
20+
echo $exception->getAPIMessage() . "\n";
21+
echo $exception->getAPICode() . "\n";
22+
echo $exception->getAPIDescription() . "\n";
2123
}
2224
?>

examples/transmission/get_transmission.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
$results = $sparky->transmission->find('Your Transmission ID');
1818
echo 'Congrats! You retrieved your transmission from SparkPost!';
1919
} catch (\Exception $exception) {
20-
echo $exception->getMessage();
20+
echo $exception->getAPIMessage() . "\n";
21+
echo $exception->getAPICode() . "\n";
22+
echo $exception->getAPIDescription() . "\n";
2123
}
2224
?>

examples/transmission/rfc822.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
]);
2727
echo 'Congrats! You sent an email using SparkPost!';
2828
} catch (\Exception $exception) {
29-
echo $exception->getMessage();
29+
echo $exception->getAPIMessage() . "\n";
30+
echo $exception->getAPICode() . "\n";
31+
echo $exception->getAPIDescription() . "\n";
3032
}
3133
?>

examples/transmission/send_transmission_all_fields.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

6262
echo 'Congrats! You sent an email using SparkPost!';
6363
} catch (\Exception $exception) {
64-
echo $exception->getMessage();
64+
echo $exception->getAPIMessage() . "\n";
65+
echo $exception->getAPICode() . "\n";
66+
echo $exception->getAPIDescription() . "\n";
6567
}
6668
?>

examples/transmission/simple_send.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
]);
3333
echo 'Congrats! You sent an email using SparkPost!';
3434
} catch (\Exception $exception) {
35-
echo $exception->getMessage();
35+
echo $exception->getAPIMessage() . "\n";
36+
echo $exception->getAPICode() . "\n";
37+
echo $exception->getAPIDescription() . "\n";
3638
}
3739
?>

examples/transmission/stored_recipients_inline_content.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
echo 'Congrats! You sent an email using SparkPost!';
3131
} catch (\Exception $exception) {
32-
echo $exception->getMessage();
32+
echo $exception->getAPIMessage() . "\n";
33+
echo $exception->getAPICode() . "\n";
34+
echo $exception->getAPIDescription() . "\n";
3335
}
3436
?>

examples/transmission/stored_template_send.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
]);
3131
echo 'Congrats! You sent an email using SparkPost!';
3232
} catch (\Exception $exception) {
33-
echo $exception->getMessage();
33+
echo $exception->getAPIMessage() . "\n";
34+
echo $exception->getAPICode() . "\n";
35+
echo $exception->getAPIDescription() . "\n";
3436
}
3537
?>

lib/SparkPost/APIResource.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function delete( $resourcePath=null, Array $query=[] ) {
131131
* assembles a URL for a request
132132
* @param string $resourcePath path after the initial endpoint
133133
* @param array $options array with an optional value of query with values to build a querystring from. Any
134-
* query elements that are themselves arrays will be imploded into a comma separated list.
134+
* query elements that are themselves arrays will be imploded into a comma separated list.
135135
* @return string the assembled URL
136136
*/
137137
private function buildUrl($resourcePath, $options) {
@@ -202,7 +202,14 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
202202
return json_decode($response->getBody()->getContents(), true);
203203
}
204204
elseif ($statusCode === 403) {
205-
throw new APIResponseException('Request forbidden. Does this API Key have the correct SparkPost permissions?');
205+
$response = json_decode($response->getBody(), true);
206+
throw new APIResponseException(
207+
'Request forbidden',
208+
$statusCode,
209+
isset($response['errors'][0]['message']) ? $response['errors'][0]['message'] : "Request forbidden",
210+
isset($response['errors'][0]['code']) ? $response['errors'][0]['code'] : 1100,
211+
isset($response['errors'][0]['description']) ? $response['errors'][0]['description'] : "Does this API Key have the correct permissions?"
212+
);
206213
}
207214
elseif ($statusCode === 404) {
208215
throw new APIResponseException('The specified resource does not exist', 404);

test/unit/APIResourceTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,18 @@ public function testAdapter404Exception() {
131131
}
132132

133133
public function testAdapter403Exception() {
134+
$testBody = [ 'errors' => [
135+
[
136+
'message' => 'Forbidden.'
137+
]
138+
]];
134139
try {
135140
$responseMock = Mockery::mock();
136141
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
137142
once()->
138143
andReturn($responseMock);
139144
$responseMock->shouldReceive('getStatusCode')->andReturn(403);
145+
$responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody));
140146

141147
$this->resource->get('test');
142148
}

0 commit comments

Comments
 (0)