|
7 | 7 |
|
8 | 8 | namespace PHPShopify; |
9 | 9 |
|
| 10 | +use PHPShopify\Exception\ApiException; |
| 11 | + |
10 | 12 | /** |
11 | 13 | * Class HttpRequestJson |
12 | 14 | * |
@@ -183,31 +185,34 @@ public static function shouldRetry($response, $error, $retry) { |
183 | 185 | * |
184 | 186 | * @param string $response |
185 | 187 | * |
| 188 | + * @throws ApiException |
| 189 | + * |
186 | 190 | * @return array |
187 | 191 | */ |
188 | 192 | protected static function processResponse($response) |
189 | 193 | { |
190 | 194 | $responseArray = json_decode($response, true); |
191 | 195 |
|
192 | | - if ($responseArray === null) { |
193 | | - //Something went wrong, Checking HTTP Codes |
194 | | - $httpOK = 200; //Request Successful, OK. |
195 | | - $httpCreated = 201; //Create Successful. |
196 | | - $httpDeleted = 204; //Delete Successful |
197 | | - $httpOther = 303; //See other (headers). |
| 196 | + //Something went wrong, Checking HTTP Codes |
| 197 | + $httpOK = 200; //Request Successful, OK. |
| 198 | + $httpCreated = 201; //Create Successful. |
| 199 | + $httpDeleted = 204; //Delete Successful |
| 200 | + $httpOther = 303; //See other (headers). |
198 | 201 |
|
199 | | - $lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders; |
| 202 | + $lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders; |
200 | 203 |
|
201 | | - //should be null if any other library used for http calls |
202 | | - $httpCode = CurlRequest::$lastHttpCode; |
| 204 | + //should be null if any other library used for http calls |
| 205 | + $httpCode = CurlRequest::$lastHttpCode; |
203 | 206 |
|
204 | | - if ($httpCode == $httpOther && array_key_exists('location', $lastHttpResponseHeaders)) { |
205 | | - return ['location' => $lastHttpResponseHeaders['location']]; |
206 | | - } |
| 207 | + if ($httpCode == $httpOther && array_key_exists('location', $lastHttpResponseHeaders)) { |
| 208 | + return ['location' => $lastHttpResponseHeaders['location']]; |
| 209 | + } |
207 | 210 |
|
208 | | - if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) { |
209 | | - throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode); |
210 | | - } |
| 211 | + if (!in_array($httpCode, [null, $httpOK, $httpCreated, $httpDeleted]) || !empty($responseArray['error'])) { |
| 212 | + $message = "Request failed" |
| 213 | + . ($httpCode ? " with HTTP Code $httpCode" : "") |
| 214 | + . (!empty($responseArray['error']) ? ': ' . $responseArray['error'] : '.'); |
| 215 | + throw new ApiException($message, $httpCode); |
211 | 216 | } |
212 | 217 |
|
213 | 218 | return $responseArray; |
|
0 commit comments