Skip to content

Commit

Permalink
Added better context to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Oct 1, 2019
1 parent 03caa8f commit 80db253
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/DataProvider/DataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getClass(): string;
/**
* Will return an iterable of ids
*
* @return iterable<CollectionBatchInterface>
* @return CollectionBatchInterface[]
*/
public function getBatches(ChannelInterface $channel, LocaleInterface $locale): iterable;

Expand All @@ -32,7 +32,7 @@ public function getBatchCount(ChannelInterface $channel, LocaleInterface $locale
/**
* This will return the items based on the given batch
*
* @return iterable<ResourceInterface>
* @return ResourceInterface[]
*/
public function getItems(BatchInterface $batch): iterable;
}
45 changes: 28 additions & 17 deletions src/Exception/GenerateBatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion src/Message/Handler/GenerateBatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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(), [
Expand Down

0 comments on commit 80db253

Please sign in to comment.