|
3 | 3 | use Ivory\HttpAdapter\HttpAdapterException; |
4 | 4 | use SparkPost\SparkPost; |
5 | 5 |
|
| 6 | + |
| 7 | + |
6 | 8 | /** |
7 | 9 | * @desc SDK interface for managing SparkPost API endpoints |
8 | 10 | */ |
@@ -186,24 +188,29 @@ private function callResource( $action, $resourcePath=null, $options=[] ) { |
186 | 188 | //make request |
187 | 189 | try { |
188 | 190 | $response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body); |
189 | | - return json_decode($response->getBody()->getContents(), true); |
190 | | - } |
191 | | - /* |
192 | | - * Handles 4XX responses |
193 | | - */ |
194 | | - catch (HttpAdapterException $exception) { |
195 | | - $response = $exception->getResponse(); |
| 191 | + |
196 | 192 | $statusCode = $response->getStatusCode(); |
197 | | - if($statusCode === 404) { |
198 | | - throw new \Exception('The specified resource does not exist', 404); |
| 193 | + |
| 194 | + // Handle 4XX responses, 5XX responses will throw an HttpAdapterException |
| 195 | + if ($statusCode < 400) { |
| 196 | + return json_decode($response->getBody()->getContents(), true); |
| 197 | + } else { |
| 198 | + if ($statusCode === 404) { |
| 199 | + throw new APIResponseException('The specified resource does not exist', 404); |
| 200 | + } |
| 201 | + throw new APIResponseException('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode ); |
199 | 202 | } |
200 | | - throw new \Exception('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode ); |
201 | 203 | } |
| 204 | + |
202 | 205 | /* |
203 | | - * Handles 5XX Errors, Configuration Errors, and a catch all for other errors |
| 206 | + * Configuration Errors, and a catch all for other errors |
204 | 207 | */ |
205 | 208 | catch (\Exception $exception) { |
206 | | - throw new \Exception('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage()); |
| 209 | + if($exception instanceof APIResponseException) { |
| 210 | + throw $exception; |
| 211 | + } |
| 212 | + |
| 213 | + throw new APIResponseException('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage()); |
207 | 214 | } |
208 | 215 | } |
209 | 216 |
|
|
0 commit comments