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 33e1daf commit 03caa8f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 21 deletions.
84 changes: 84 additions & 0 deletions src/Exception/GenerateBatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusFeedPlugin\Exception;

use RuntimeException;
use Throwable;

final class GenerateBatchException extends RuntimeException implements ExceptionInterface
{
private const DEFAULT_MESSAGE = 'An error occurred';

/**
* @var int
*/
private $feedId;

/**
* @var string
*/
private $channelCode;

/**
* @var string
*/
private $localeCode;

public function __construct(string $message, Throwable $previous)
{
parent::__construct($message, 0, $previous);
}

public function getFeedId(): int
{
return $this->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;
}
}
}
65 changes: 44 additions & 21 deletions src/Message/Handler/GenerateBatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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()]);

Expand Down

0 comments on commit 03caa8f

Please sign in to comment.