|
1 | 1 | <?php |
2 | 2 | namespace SparkPost; |
3 | 3 | use Guzzle\Http\Client; |
4 | | -use Guzzle\Http\Exception\RequestException; |
| 4 | +use Guzzle\Http\Exception\ClientErrorResponseException; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * @desc SDK interface for managing transmissions |
@@ -98,18 +98,6 @@ private static function getBaseUrl($config) { |
98 | 98 | return $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . $baseUrl; |
99 | 99 | } |
100 | 100 |
|
101 | | - /** |
102 | | - * Helper function for extracting the response status code out of request thrown exceptions |
103 | | - * @param string $exceptionMessage |
104 | | - * @return number |
105 | | - */ |
106 | | - private static function getStatusCode ($exceptionMessage) { |
107 | | - $messageParts = explode("\n", $exceptionMessage); |
108 | | - $codeLine = explode('] ', $messageParts[1]); |
109 | | - $code = trim($codeLine[1]); |
110 | | - return (int)$code; |
111 | | - } |
112 | | - |
113 | 101 | /** |
114 | 102 | * @desc Method for issuing POST request to the Transmissions API |
115 | 103 | * |
@@ -150,11 +138,19 @@ public static function send($transmissionConfig) { |
150 | 138 | try { |
151 | 139 | $response = $request->post(self::getBaseUrl($hostConfig), array('authorization' => $hostConfig['key']), json_encode($model), array("verify"=>$hostConfig['strictSSL']))->send(); |
152 | 140 | return $response->json(); |
153 | | - } catch (RequestException $exception) { |
| 141 | + } |
| 142 | + /* |
| 143 | + * Handles 4XX responses |
| 144 | + */ |
| 145 | + catch (ClientErrorResponseException $exception) { |
154 | 146 | $response = $exception->getResponse(); |
155 | 147 | $responseArray = $response->json(); |
156 | | - throw new \Exception(json_encode($responseArray['errors'])); |
157 | | - } catch (\Exception $exception) { |
| 148 | + throw new \Exception(json_encode($responseArray['errors'])); |
| 149 | + } |
| 150 | + /* |
| 151 | + * Handles 5XX Errors, Configuration Errors, and a catch all for other errors |
| 152 | + */ |
| 153 | + catch (\Exception $exception) { |
158 | 154 | throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage()); |
159 | 155 | } |
160 | 156 | } |
@@ -184,15 +180,23 @@ private static function fetch ($transmissionID = null) { |
184 | 180 | try { |
185 | 181 | $response = $request->get($url, array('authorization' => $hostConfig['key']), array("verify"=>$hostConfig['strictSSL']))->send(); |
186 | 182 | return $response->json(); |
187 | | - } catch (\Exception $exception) { |
188 | | - $statusCode = self::getStatusCode($exception->getMessage()); |
| 183 | + } |
| 184 | + /* |
| 185 | + * Handles 4XX responses |
| 186 | + */ |
| 187 | + catch (ClientErrorResponseException $exception) { |
| 188 | + $response = $exception->getResponse(); |
| 189 | + $statusCode = $response->getStatusCode(); |
189 | 190 | if($statusCode === 404) { |
190 | 191 | throw new \Exception("The specified Transmission ID does not exist", 404); |
191 | | - }else if ($statusCode !== null){ |
192 | | - throw new \Exception("Received bad response from Transmission API: ". $statusCode); |
193 | | - } else { |
194 | | - throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage()); |
195 | 192 | } |
| 193 | + throw new \Exception("Received bad response from Transmission API: ". $statusCode); |
| 194 | + } |
| 195 | + /* |
| 196 | + * Handles 5XX Errors, Configuration Errors, and a catch all for other errors |
| 197 | + */ |
| 198 | + catch (\Exception $exception) { |
| 199 | + throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage()); |
196 | 200 | } |
197 | 201 | } |
198 | 202 |
|
|
0 commit comments