Skip to content

Commit

Permalink
added catching and logging of bad requests to LuigisBoxClient
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Mar 12, 2024
1 parent c64bf13 commit db1f234
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
67 changes: 52 additions & 15 deletions src/Component/LuigisBox/LuigisBoxClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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'];
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit db1f234

Please sign in to comment.