Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/wbs24.ozonapinew/lib/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew; use Bitrix\Main\Localization\Loc; class ErrorProcessing { protected const UPDATE_STOCKS_URL = '/v2/products/stocks'; protected const UPDATE_PRICES_URL = '/v1/product/import/prices'; protected const DEFAULT_ENTITY_TYPE = 'order'; // коды ошибок, которые не являются ошибочными. protected $skipErrorCodes = [ 5 ]; protected $urlsToEntityType = [ // prices '/v1/product/import/prices' => 'price', '/v5/product/info/prices' => 'price', // stocks '/v3/product/info/list' => 'stock', '/v3/product/list' => 'stock', '/v2/products/stocks' => 'stock', '/v4/product/info/stocks' => 'stock', ]; public function __construct($objects = []) { try { $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects); $this->LogStack = $objects['LogStack'] ?? new LogStack(); $this->accountIndex = $this->wrappers->Option->getAccountIndex(); $this->testMode = $this->wrappers->Option->get($this->moduleId, 'testMode'); } catch (SystemException $exception) { $this->exceptionHandler($exception); } } public function verifyErrors($request, $response, $url, $baseUrl) { if ( !empty($response['message']) && !in_array($response['code'], $this->skipErrorCodes) ) { $criticalError = true; } else { $criticalError = false; } switch ($criticalError) { case true: $this->handleCriticalErrors( // отсечь критичные ошибки $request, $response, $this->prepareUrl($url, $baseUrl) ); break; case false: $this->handlePossibleErrors( // отсечь возможные ошибки $request, $response, $this->prepareUrl($url, $baseUrl) ); break; } $this->LogStack->rotateLogs(); } protected function handleCriticalErrors($request, $response, $url) { $suffix = strtoupper($this->moduleId); $detailMessage = $response['details']['value'] ?? ''; if ($detailMessage) { $errorMessage = Loc::getMessage($suffix.'.ERROR_DESCRIPTION') . ' ' . $response['message'] . '<br><br>'; $errorMessage .= Loc::getMessage($suffix.'.ERROR_DETAIL'). ' ' . $detailMessage; } else { $errorMessage = Loc::getMessage($suffix.'.ERROR_DESCRIPTION') . ' ' . $response['message'] . '<br>'; } $this->sendErrorsToLogging([ 'account_index' => $this->accountIndex, 'entity_type' => $this->urlsToEntityType[$url] ?? self::DEFAULT_ENTITY_TYPE, 'log_type' => 'error', 'api_method' => $url, 'message' => $errorMessage, 'api_request' => json_encode($request, JSON_UNESCAPED_UNICODE), 'api_response' => json_encode($response, JSON_UNESCAPED_UNICODE), 'trace' => '', ]); } protected function handlePossibleErrors($request, $response, $url) { switch ($url) { case self::UPDATE_STOCKS_URL: $this->checkAndSendErrorMessages($request, $response, $url); break; case self::UPDATE_PRICES_URL: $this->checkAndSendErrorMessages($request, $response, $url); break; } } protected function sendErrorsToLogging($log) { $this->LogStack->create($log); } protected function checkAndSendErrorMessages($request, $response, $url) { $suffix = strtoupper($this->moduleId); $errorMessage = ''; foreach ($response['result'] as $updatedProduct) { if (!empty($updatedProduct['errors'])) { foreach ($updatedProduct['errors'] as $error) { $errorMessage = Loc::getMessage($suffix.'.ERROR_START_MESSAGE') . ' ' . $updatedProduct['offer_id'] . '<br><br>' . ' ' . Loc::getMessage($suffix.'.ERROR_CODE') . ' ' . $error['code'] . '<br><br>' . ' ' . Loc::getMessage($suffix.'.ERROR_DESCRIPTION') . ' ' . $error['message'] ; $this->sendErrorsToLogging([ 'account_index' => $this->accountIndex, 'entity_type' => $this->urlsToEntityType[$url] ?? self::DEFAULT_ENTITY_TYPE, 'log_type' => 'error', 'api_method' => $url, 'message' => $errorMessage, 'api_request' => json_encode($request, JSON_UNESCAPED_UNICODE), 'api_response' => json_encode($response, JSON_UNESCAPED_UNICODE), 'trace' => '', ]); } } } } function prepareUrl($url, $baseUrl) { $url = str_replace('https://api-seller.ozon.ru', '', $url); if ($this->testMode == 'Y') { $url = str_replace($baseUrl, '', $url); $url = str_replace('.php', '', $url); } return $url; } }