diff --git a/src/Exception/GenerateBatchException.php b/src/Exception/GenerateBatchException.php new file mode 100644 index 00000000..1474f4e5 --- /dev/null +++ b/src/Exception/GenerateBatchException.php @@ -0,0 +1,84 @@ +feedId; + } + + public function setFeedId(int $feedId): void + { + $this->feedId = $feedId; + + $this->updateMessage(); + } + + public function getChannelCode(): string + { + return $this->channelCode; + } + + public function setChannelCode(string $channelCode): void + { + $this->channelCode = $channelCode; + + $this->updateMessage(); + } + + public function getLocaleCode(): string + { + return $this->localeCode; + } + + public function setLocaleCode(string $localeCode): void + { + $this->localeCode = $localeCode; + + $this->updateMessage(); + } + + private function updateMessage(): void + { + if(null !== $this->feedId) { + $this->message .= ' | Feed: '.$this->feedId; + } + + if(null !== $this->channelCode) { + $this->message .= ' | Channel code: '.$this->channelCode; + } + + if(null !== $this->localeCode) { + $this->message .= ' | Locale code: '.$this->localeCode; + } + } +} diff --git a/src/Message/Handler/GenerateBatchHandler.php b/src/Message/Handler/GenerateBatchHandler.php index 57723d12..0cb7da98 100644 --- a/src/Message/Handler/GenerateBatchHandler.php +++ b/src/Message/Handler/GenerateBatchHandler.php @@ -18,6 +18,7 @@ use Setono\SyliusFeedPlugin\Event\BatchGeneratedEvent; use Setono\SyliusFeedPlugin\Event\GenerateBatchItemEvent; use Setono\SyliusFeedPlugin\Event\GenerateBatchViolationEvent; +use Setono\SyliusFeedPlugin\Exception\GenerateBatchException; use Setono\SyliusFeedPlugin\Factory\ViolationFactoryInterface; use Setono\SyliusFeedPlugin\Generator\FeedPathGeneratorInterface; use Setono\SyliusFeedPlugin\Generator\TemporaryFeedPathGenerator; @@ -126,29 +127,37 @@ public function __invoke(GenerateBatch $message): void $stream = $this->openStream(); foreach ($items as $item) { - $contextList = $itemContext->getContextList($item, $channel, $locale); - foreach ($contextList as $context) { - $this->eventDispatcher->dispatch(new GenerateBatchItemEvent( - $feed, $feedType, $channel, $locale, $context - )); - - $constraintViolationList = $this->validator->validate( - $context, null, $feedType->getValidationGroups() - ); - if ($constraintViolationList->count() > 0) { - foreach ($constraintViolationList as $constraintViolation) { - $violation = $this->violationFactory->createFromConstraintViolation( - $constraintViolation, $channel, $locale, $context - ); - - $feed->addViolation($violation); - } - - $this->eventDispatcher->dispatch(new GenerateBatchViolationEvent( - $feed, $feedType, $channel, $locale, $context, $constraintViolationList + try { + $contextList = $itemContext->getContextList($item, $channel, $locale); + foreach ($contextList as $context) { + $this->eventDispatcher->dispatch(new GenerateBatchItemEvent( + $feed, $feedType, $channel, $locale, $context )); + + $constraintViolationList = $this->validator->validate( + $context, null, $feedType->getValidationGroups() + ); + if ($constraintViolationList->count() > 0) { + foreach ($constraintViolationList as $constraintViolation) { + $violation = $this->violationFactory->createFromConstraintViolation( + $constraintViolation, $channel, $locale, $context + ); + + $feed->addViolation($violation); + } + + $this->eventDispatcher->dispatch(new GenerateBatchViolationEvent( + $feed, $feedType, $channel, $locale, $context, $constraintViolationList + )); + } + fwrite($stream, $template->renderBlock('item', ['item' => $context])); } - fwrite($stream, $template->renderBlock('item', ['item' => $context])); + } catch (Throwable $e) { + $newException = new GenerateBatchException($e->getMessage(), $e); + $newException->setChannelCode($channel->getCode()); + $newException->setLocaleCode($locale->getCode()); + + throw $newException; } } @@ -162,6 +171,20 @@ public function __invoke(GenerateBatch $message): void Assert::true($res, 'An error occurred when trying to write a feed item'); $this->eventDispatcher->dispatch(new BatchGeneratedEvent($feed)); + } catch(GenerateBatchException $e) { + $e->setFeedId($feed->getId()); + + $this->logger->critical($e->getMessage(), [ + 'feedId' => $feed->getId(), + 'channelCode' => $e->getChannelCode(), + 'localeCode' => $e->getLocaleCode(), + ]); + + $this->applyErrorTransition($workflow, $feed); + + $this->feedManager->flush(); + + throw $e; } catch (Throwable $e) { $this->logger->critical($e->getMessage(), ['feedId' => $feed->getId()]);