Skip to content

Commit 51c084f

Browse files
committed
Better handling of response and error
1 parent 2ab054e commit 51c084f

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/CurlRequest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
*/
2121
class CurlRequest
2222
{
23+
/**
24+
* HTTP Code of the last executed request
25+
*
26+
* @var integer
27+
*/
28+
public static $lastHttpCode;
29+
30+
2331
/**
2432
* Initialize the curl resource
2533
*
@@ -28,7 +36,7 @@ class CurlRequest
2836
*
2937
* @return resource
3038
*/
31-
public static function init($url, $httpHeaders = array())
39+
protected static function init($url, $httpHeaders = array())
3240
{
3341
// Create Curl resource
3442
$ch = curl_init();
@@ -130,7 +138,7 @@ public static function delete($url, $httpHeaders = array())
130138
*
131139
* @return string
132140
*/
133-
public static function processRequest($ch)
141+
protected static function processRequest($ch)
134142
{
135143
// $output contains the output string
136144
$output = curl_exec($ch);
@@ -139,13 +147,7 @@ public static function processRequest($ch)
139147
throw new Exception\CurlException(curl_errno($ch) . ' : ' . curl_error($ch));
140148
}
141149

142-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
143-
$httpOK = 200;
144-
$httpCreated = 201;
145-
146-
if ($httpCode != $httpOK && $httpCode != $httpCreated) {
147-
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.");
148-
}
150+
self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
149151

150152
// close curl resource to free up system resources
151153
curl_close($ch);

lib/ShopifyAPI.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,13 @@ protected function castString($array)
453453
if (is_string($array)) return $array;
454454

455455
$string = '';
456-
456+
$i = 0;
457457
foreach ($array as $key => $val) {
458-
$string .= "$key - " . $this->castString($val) . ', ';
458+
//Add values separated by comma
459+
//prepend the key string, if it's an associative key
460+
//Check if the value itself is another array to be converted to string
461+
$string .= ($i === $key ? '' : "$key - ") . $this->castString($val) . ', ';
462+
$i++;
459463
}
460464

461465
//Remove trailing comma and space
@@ -478,6 +482,17 @@ public function processResponse($response, $dataKey = false)
478482
{
479483
$responseArray = json_decode($response, true);
480484

485+
if($responseArray === null) {
486+
//Something went wrong, Checking HTTP Codes
487+
$httpOK = 200; //Request Successful, OK.
488+
$httpCreated = 201; //Create Successful.
489+
$httpCode = CurlRequest::$lastHttpCode;
490+
491+
if ($httpCode != $httpOK && $httpCode != $httpCreated) {
492+
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.");
493+
}
494+
}
495+
481496
if (isset ($responseArray['errors'])) {
482497
$message = $this->castString($responseArray['errors']);
483498

0 commit comments

Comments
 (0)