From 80db2538d5c865a3780092996e50923f0b9c1c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 1 Oct 2019 13:29:05 +0200 Subject: [PATCH] Added better context to exceptions --- src/DataProvider/DataProviderInterface.php | 4 +- src/Exception/GenerateBatchException.php | 45 ++++++++++++-------- src/Message/Handler/GenerateBatchHandler.php | 3 +- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/DataProvider/DataProviderInterface.php b/src/DataProvider/DataProviderInterface.php index 5688bdcc..43aaaf48 100644 --- a/src/DataProvider/DataProviderInterface.php +++ b/src/DataProvider/DataProviderInterface.php @@ -20,7 +20,7 @@ public function getClass(): string; /** * Will return an iterable of ids * - * @return iterable + * @return CollectionBatchInterface[] */ public function getBatches(ChannelInterface $channel, LocaleInterface $locale): iterable; @@ -32,7 +32,7 @@ public function getBatchCount(ChannelInterface $channel, LocaleInterface $locale /** * This will return the items based on the given batch * - * @return iterable + * @return ResourceInterface[] */ public function getItems(BatchInterface $batch): iterable; } diff --git a/src/Exception/GenerateBatchException.php b/src/Exception/GenerateBatchException.php index 1474f4e5..9d5ee139 100644 --- a/src/Exception/GenerateBatchException.php +++ b/src/Exception/GenerateBatchException.php @@ -9,21 +9,16 @@ final class GenerateBatchException extends RuntimeException implements ExceptionInterface { - private const DEFAULT_MESSAGE = 'An error occurred'; - - /** - * @var int - */ + /** @var int */ private $feedId; - /** - * @var string - */ + /** @var int */ + private $resourceId; + + /** @var string */ private $channelCode; - /** - * @var string - */ + /** @var string */ private $localeCode; public function __construct(string $message, Throwable $previous) @@ -43,6 +38,18 @@ public function setFeedId(int $feedId): void $this->updateMessage(); } + public function getResourceId(): int + { + return $this->resourceId; + } + + public function setResourceId(int $resourceId): void + { + $this->resourceId = $resourceId; + + $this->updateMessage(); + } + public function getChannelCode(): string { return $this->channelCode; @@ -69,16 +76,20 @@ public function setLocaleCode(string $localeCode): void private function updateMessage(): void { - if(null !== $this->feedId) { - $this->message .= ' | Feed: '.$this->feedId; + if (null !== $this->feedId) { + $this->message .= ' | Feed: ' . $this->feedId; + } + + if (null !== $this->resourceId) { + $this->message .= ' | Resource id: ' . $this->resourceId; } - if(null !== $this->channelCode) { - $this->message .= ' | Channel code: '.$this->channelCode; + if (null !== $this->channelCode) { + $this->message .= ' | Channel code: ' . $this->channelCode; } - if(null !== $this->localeCode) { - $this->message .= ' | Locale code: '.$this->localeCode; + 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 0cb7da98..d603a5c2 100644 --- a/src/Message/Handler/GenerateBatchHandler.php +++ b/src/Message/Handler/GenerateBatchHandler.php @@ -154,6 +154,7 @@ public function __invoke(GenerateBatch $message): void } } catch (Throwable $e) { $newException = new GenerateBatchException($e->getMessage(), $e); + $newException->setResourceId($item->getId()); $newException->setChannelCode($channel->getCode()); $newException->setLocaleCode($locale->getCode()); @@ -171,7 +172,7 @@ 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) { + } catch (GenerateBatchException $e) { $e->setFeedId($feed->getId()); $this->logger->critical($e->getMessage(), [