1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 10:07:49 +01:00
parallel/lib/Sync/ExitFailure.php
Aaron Piotrowski 1ee8e202ed
Apply the same treatment to context exceptions
Sends info about previous exceptions thrown in the context to the parent.
2019-01-25 17:53:19 -06:00

59 lines
1.3 KiB
PHP

<?php
namespace Amp\Parallel\Sync;
final class ExitFailure implements ExitResult
{
/** @var string */
private $type;
/** @var string */
private $message;
/** @var int|string */
private $code;
/** @var array */
private $trace;
/** @var self|null */
private $previous;
public function __construct(\Throwable $exception)
{
$this->type = \get_class($exception);
$this->message = $exception->getMessage();
$this->code = $exception->getCode();
$this->trace = $exception->getTraceAsString();
if ($previous = $exception->getPrevious()) {
$this->previous = new self($previous);
}
}
/**
* {@inheritdoc}
*/
public function getResult()
{
throw $this->createException();
}
private function createException(): PanicError
{
$previous = $this->previous ? $this->previous->createException() : null;
return new PanicError(
$this->type,
\sprintf(
'Uncaught %s in execution context with message "%s" and code "%s"',
$this->type,
$this->message,
$this->code
),
$this->trace,
$previous
);
}
}