Skip to content

Commit b91a2f6

Browse files
committed
Improve APIResponseException class
1 parent e3f286a commit b91a2f6

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-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 $apiMessageDescription;
20+
21+
/**
22+
* Construct the exception.
23+
*/
24+
public function __construct($message = "", $code = 0, $apiMessage = "", $apiCode = 0, $apiMessageDescription = "") {
25+
$this->apiMessage = $apiMessage;
26+
$this->apiCode = $apiCode;
27+
$this->apiMessageDescription = $apiMessageDescription;
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 message
49+
* @return string the Exception message as a string.
50+
*/
51+
public function getAPIMessageDescription() {
52+
return $this->apiMessageDescription;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)