Skip to content

Commit

Permalink
Added the possibility to define a custom item index for exception logs (
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremycr authored Jul 27, 2023
1 parent 1b2b1be commit 3c56a90
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 4.1.0

* Added custom index for exception log

# Version 4.0.0

* Added Symfony 6 support
Expand Down
25 changes: 23 additions & 2 deletions src/DataflowType/Dataflow/Dataflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Dataflow implements DataflowInterface, LoggerAwareInterface
/** @var WriterInterface[] */
private array $writers = [];

private ?\Closure $customExceptionIndex = null;

public function __construct(private iterable $reader, private ?string $name)
{
}
Expand All @@ -43,6 +45,16 @@ public function addWriter(WriterInterface $writer): self
return $this;
}

/**
* @return $this
*/
public function setCustomExceptionIndex(callable $callable): self
{
$this->customExceptionIndex = \Closure::fromCallable($callable);

return $this;
}

/**
* {@inheritdoc}
*/
Expand All @@ -61,8 +73,17 @@ public function process(): Result
try {
$this->processItem($item);
} catch (\Throwable $e) {
$exceptions[$index] = $e;
$this->logException($e, (string) $index);
$exceptionIndex = $index;
try {
if (is_callable($this->customExceptionIndex)) {
$exceptionIndex = (string) ($this->customExceptionIndex)($item, $index);
}
} catch (\Throwable $e2) {
$exceptions[$index] = $e2;
$this->logException($e2, $index);
}
$exceptions[$exceptionIndex] = $e;
$this->logException($e, $exceptionIndex);
}

++$count;
Expand Down
13 changes: 13 additions & 0 deletions src/DataflowType/DataflowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class DataflowBuilder
/** @var WriterInterface[] */
private array $writers = [];

private ?\Closure $customExceptionIndex = null;

public function setName(string $name): self
{
$this->name = $name;
Expand Down Expand Up @@ -47,6 +49,13 @@ public function addWriter(WriterInterface $writer): self
return $this;
}

public function setCustomExceptionIndex(callable $callable): self
{
$this->customExceptionIndex = \Closure::fromCallable($callable);

return $this;
}

public function getDataflow(): DataflowInterface
{
$dataflow = new Dataflow($this->reader, $this->name);
Expand All @@ -62,6 +71,10 @@ public function getDataflow(): DataflowInterface
$dataflow->addWriter($writer);
}

if (is_callable($this->customExceptionIndex)) {
$dataflow->setCustomExceptionIndex($this->customExceptionIndex);
}

return $dataflow;
}
}

0 comments on commit 3c56a90

Please sign in to comment.