diff --git a/src/Component/LuigisBox/LuigisBoxClient.php b/src/Component/LuigisBox/LuigisBoxClient.php index 33c2a7a..43a9007 100644 --- a/src/Component/LuigisBox/LuigisBoxClient.php +++ b/src/Component/LuigisBox/LuigisBoxClient.php @@ -12,6 +12,8 @@ use Shopsys\LuigisBoxBundle\Component\LuigisBox\Exception\LuigisBoxActionNotRecognizedException; use Shopsys\LuigisBoxBundle\Component\LuigisBox\Exception\LuigisBoxIndexNotRecognizedException; use Shopsys\ProductFeed\LuigisBoxBundle\Model\FeedItem\LuigisBoxProductFeedItem; +use Symfony\Bridge\Monolog\Logger; +use Throwable; class LuigisBoxClient { @@ -27,11 +29,13 @@ class LuigisBoxClient * @param string $luigisBoxApiUrl * @param array $trackerIdsByDomainIds * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain + * @param \Symfony\Bridge\Monolog\Logger $logger */ public function __construct( protected readonly string $luigisBoxApiUrl, protected readonly array $trackerIdsByDomainIds, protected readonly Domain $domain, + protected readonly Logger $logger, ) { } @@ -74,22 +78,40 @@ public function getData( $this->checkNecessaryConfigurationIsSet(); $this->validateActionIsValid($action); - $data = json_decode( - file_get_contents( - $this->getLuigisBoxApiUrl( - $query, - $action, - $page, - $limitsByType, - $filter, - $userIdentifier, - $orderingMode, + try { + $data = json_decode( + file_get_contents( + $this->getLuigisBoxApiUrl( + $query, + $action, + $page, + $limitsByType, + $filter, + $userIdentifier, + $orderingMode, + ), ), - ), - true, - 512, - JSON_THROW_ON_ERROR, - ); + true, + 512, + JSON_THROW_ON_ERROR, + ); + } catch (Throwable $e) { + $this->logger->error( + 'Luigi\'s Box API request failed.', + [ + 'exception' => $e, + 'query' => $query, + 'action' => $action, + 'page' => $page, + 'limitsByType' => $limitsByType, + 'filter' => $filter, + 'userIdentifier' => $userIdentifier, + 'orderingMode' => $orderingMode, + ], + ); + + return $this->getEmptyResults(array_keys($limitsByType)); + } if ($action === self::ACTION_SEARCH) { $data = $data['results']; @@ -98,6 +120,21 @@ public function getData( return $this->getResultsIndexedByItemType($data, $action, array_keys($limitsByType)); } + /** + * @param string[] $types + * @return \Shopsys\LuigisBoxBundle\Component\LuigisBox\LuigisBoxResult[] + */ + protected function getEmptyResults(array $types): array + { + $resultsByType = []; + + foreach ($types as $type) { + $resultsByType[$type] = new LuigisBoxResult([], [], 0); + } + + return $resultsByType; + } + /** * @param array $data * @param string $action diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 9472536..f0452c1 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -13,6 +13,7 @@ services: arguments: $luigisBoxApiUrl: '%env(string:LUIGIS_BOX_API_URL)%' $trackerIdsByDomainIds: '%env(json:LUIGIS_BOX_TRACKER_IDS_BY_DOMAIN_IDS)%' + $logger: '@monolog.logger' Shopsys\LuigisBoxBundle\Model\Product\ProductSearchResultsProvider: arguments: