https://community.shopify.com/c/Shopify-APIs-SDKs/Periodic-error-when-creating-Custom-Collection-quot-expected/td-p/697730
I have tried some try catch script but no luck. It seem Shopify throw error that can not catch or something else. I do not have much knowledge about PHP try catch exception.
So I use simple way to detect error response and then sleep for a while then retry again. Because of this issue happen periodically (or may be randomly) and product data that send is OK.
Edit ShopifyResource.php (in /vendor/phpclassic/... or in shopify-laravel lib)
This function, at line about 508:
/**
* Process the request response
*
* @param array $responseArray Request response in array format
* @param string $dataKey Keyname to fetch data from response array
*
* @throws ApiException if the response has an error specified
* @throws CurlException if response received with unexpected HTTP code.
*
* @return array
*/
public function processResponse($responseArray, $dataKey = null)
{
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
if ($responseArray === null) {
//Something went wrong, Checking HTTP Codes
$httpOK = 200; //Request Successful, OK.
$httpCreated = 201; //Create Successful.
$httpDeleted = 204; //Delete Successful
//should be null if any other library used for http calls
$httpCode = CurlRequest::$lastHttpCode;
if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) {
// throw new Exception\CurlException("Request failed with HTTP Code $httpCode.");
// Append a error flag instead of throw error.
$responseArray['publication_err_flg'] = 2;
return $responseArray;
}
}
$lastResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
$this->getLinks($lastResponseHeaders);
if (isset($responseArray['errors'])) {
$message = $this->castString($responseArray['errors']);
// Append custom error flag
$responseArray['publication_err_flg'] = 1;
return $responseArray;
// throw new ApiException($message, CurlRequest::$lastHttpCode);
}
if ($dataKey && isset($responseArray[$dataKey])) {
return $responseArray[$dataKey];
} else {
return $responseArray;
}
}
In Controller (Laravel) we can use response code (error flag) to avoid crash/ throw error.
$new_collect = $this->constructShopifyCollectionSameYMMO($products);
$response = $shopify->CustomCollection->post($new_collect);
if (isset($response['publication_err_flg'])) {
$failed_collection = $new_collect;
Log::debug('Error at '. $products[0]->pro_id. " code= ". $response['publication_err_flg']);
sleep(10);
// Retry
$response = $shopify->CustomCollection->post($failed_collection);
// 3rd try
if (isset($response['publication_err_flg'])) {
Log::debug('2nd Error at '. $products[0]->pro_id. " code= ". $response['publication_err_flg']);
sleep(10);
$response3 = $shopify->CustomCollection->post($failed_collection);
if (isset($response3['publication_err_flg'])) {
Log::debug('3rd Error at '. $products[0]->pro_id. " code= ". $response['publication_err_flg']);
}
} else {
$this->logCreateCollection($response, $ymmo, $products[0]->pro_id);
}
} else {
// Handle result. TODO try-catch
Log::debug("Shopify Collect ". $ymmo['year']. "_". @$ymmo['makename']. "_". $ymmo['modelname']. "_". $ymmo['submodelname']. " Collect ID ". @$response['id']. " ". $products[0]->pro_id);
// Update shopify_collect_id back to Drilled.product
// dd($response);
usleep(500);
}
This is a dodgy work and code are not refactor and optimized. You may need to modify to suite your business logic.
Hope this help.
Comments
Post a Comment