diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/SparkPost/APIResource.php | 31 | ||||
-rw-r--r-- | lib/SparkPost/APIResponseException.php | 9 |
2 files changed, 28 insertions, 12 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index a2cd5a6..563a56e 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -3,6 +3,8 @@ namespace SparkPost; use Ivory\HttpAdapter\HttpAdapterException; use SparkPost\SparkPost; + + /** * @desc SDK interface for managing SparkPost API endpoints */ @@ -186,24 +188,29 @@ class APIResource { //make request try { $response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body); - return json_decode($response->getBody()->getContents(), true); - } - /* - * Handles 4XX responses - */ - catch (HttpAdapterException $exception) { - $response = $exception->getResponse(); + $statusCode = $response->getStatusCode(); - if($statusCode === 404) { - throw new \Exception('The specified resource does not exist', 404); + + // Handle 4XX responses, 5XX responses will throw an HttpAdapterException + if ($statusCode < 400) { + return json_decode($response->getBody()->getContents(), true); + } else { + if ($statusCode === 404) { + throw new APIResponseException('The specified resource does not exist', 404); + } + throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode ); } - throw new \Exception('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode ); } + /* - * Handles 5XX Errors, Configuration Errors, and a catch all for other errors + * Configuration Errors, and a catch all for other errors */ catch (\Exception $exception) { - throw new \Exception('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage()); + if($exception instanceof APIResponseException) { + throw $exception; + } + + throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage()); } } diff --git a/lib/SparkPost/APIResponseException.php b/lib/SparkPost/APIResponseException.php new file mode 100644 index 0000000..cc0842c --- /dev/null +++ b/lib/SparkPost/APIResponseException.php @@ -0,0 +1,9 @@ +<?php + +namespace SparkPost; + +class APIResponseException extends \Exception { + +} + +?> |