Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to process timeout exception [WIP] #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading