Skip to content

Commit

Permalink
Add context to process timeout exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiří Bok committed May 14, 2023
1 parent 2891114 commit d4b8415
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/ValueObject/ChildProcessTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyParallel\ValueObject;

use Exception;

class ChildProcessTimeoutException extends Exception
{
/**
* @var mixed[]
*/
private array $context = [];

/**
* @param mixed[] $context
*/
public function __construct(
string $message = '',
array $context = [],
int $code = 0,
?Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
$this->context = $context;
}

/**
* @return mixed[]
*/
public function getContext(): array
{
return $this->context;
}
}
5 changes: 2 additions & 3 deletions src/ValueObject/ParallelProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Exception;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
Expand Down Expand Up @@ -98,11 +97,11 @@ public function request(array $data): void
{
$this->cancelTimer();
$this->encoder->write($data);
$this->timer = $this->loop->addTimer($this->timetoutInSeconds, function (): void {
$this->timer = $this->loop->addTimer($this->timetoutInSeconds, function () use ($data): void {
$onError = $this->onError;

$errorMessage = sprintf('Child process timed out after %d seconds', $this->timetoutInSeconds);
$onError(new Exception($errorMessage));
$onError(new ChildProcessTimeoutException($errorMessage, $data));
});
}

Expand Down

0 comments on commit d4b8415

Please sign in to comment.