diff options
author | beardyman <nornholdj@gmail.com> | 2016-02-23 00:28:59 -0500 |
---|---|---|
committer | beardyman <nornholdj@gmail.com> | 2016-02-23 00:28:59 -0500 |
commit | 547f84d69ab1ca529b112aab43aa6f5b901aaa25 (patch) | |
tree | 7d55ca3e180c2a0b4eb0ac6e9b2396899de310bc | |
parent | 079c21446283a4adc3a4ac6f7c0c9ad7f86d34b7 (diff) | |
download | php-sparkpost-547f84d69ab1ca529b112aab43aa6f5b901aaa25.zip php-sparkpost-547f84d69ab1ca529b112aab43aa6f5b901aaa25.tar.gz php-sparkpost-547f84d69ab1ca529b112aab43aa6f5b901aaa25.tar.bz2 |
added logic to handle error responses
-rw-r--r-- | lib/SparkPost/APIResource.php | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index a2cd5a6..e946c0f 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()); } } |