Skip to content

Commit 3451deb

Browse files
committed
Fix HTTP error responses were handled as success when JSON encoded error info was present
1 parent e18f92e commit 3451deb

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

lib/HttpRequestJson.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace PHPShopify;
99

10+
use PHPShopify\Exception\ApiException;
11+
1012
/**
1113
* Class HttpRequestJson
1214
*
@@ -183,31 +185,34 @@ public static function shouldRetry($response, $error, $retry) {
183185
*
184186
* @param string $response
185187
*
188+
* @throws ApiException
189+
*
186190
* @return array
187191
*/
188192
protected static function processResponse($response)
189193
{
190194
$responseArray = json_decode($response, true);
191195

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).
198201

199-
$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
202+
$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
200203

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;
203206

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+
}
207210

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);
211216
}
212217

213218
return $responseArray;

0 commit comments

Comments
 (0)