From 6d0c77f5b796f1e82ed8d13329dc63c393a9720c Mon Sep 17 00:00:00 2001 From: Mathieu Santostefano Date: Mon, 21 Oct 2024 11:39:46 +0200 Subject: [PATCH] fix(populate): Better TypeError handling on AutoMapper::map before indexing --- src/Index/Indexer.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Index/Indexer.php b/src/Index/Indexer.php index c366e67..a58c301 100644 --- a/src/Index/Indexer.php +++ b/src/Index/Indexer.php @@ -28,6 +28,7 @@ use Sylius\Component\Resource\Model\TranslatableInterface; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; +use TypeError; final class Indexer implements IndexerInterface { @@ -192,7 +193,16 @@ private function indexDocumentable(OutputInterface $output, DocumentableInterfac if (null !== $locale && $item instanceof TranslatableInterface) { $item->setCurrentLocale($locale); } - $dto = $this->autoMapper->map($item, $documentable->getTargetClass()); + + try { + $dto = $this->autoMapper->map($item, $documentable->getTargetClass()); + } catch (TypeError $e) { + $id = method_exists($item, 'getId') ? $item->getId() : 'unknown'; + $output->writeln(\sprintf('Error while mapping %s (id: %s): %s', $item::class, $id, $e->getMessage())); + + continue; + } + // @phpstan-ignore-next-line $indexer->scheduleIndex($newIndex, new Document((string) $dto->getId(), $dto)); }