mirror of
https://github.com/danog/parallel.git
synced 2024-12-11 16:49:51 +01:00
70 lines
1.8 KiB
PHP
70 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Amp\Parallel\Worker\Internal;
|
|
|
|
use Amp\Failure;
|
|
use Amp\Parallel\Worker\TaskError;
|
|
use Amp\Parallel\Worker\TaskException;
|
|
use Amp\Promise;
|
|
|
|
/** @internal */
|
|
class TaskFailure extends TaskResult {
|
|
const PARENT_EXCEPTION = 0;
|
|
const PARENT_ERROR = 1;
|
|
|
|
/** @var string */
|
|
private $type;
|
|
|
|
/** @var int */
|
|
private $parent;
|
|
|
|
/** @var string */
|
|
private $message;
|
|
|
|
/** @var int|string */
|
|
private $code;
|
|
|
|
/** @var array */
|
|
private $trace;
|
|
|
|
public function __construct(string $id, \Throwable $exception) {
|
|
parent::__construct($id);
|
|
$this->type = \get_class($exception);
|
|
$this->parent = $exception instanceof \Error ? self::PARENT_ERROR : self::PARENT_EXCEPTION;
|
|
$this->message = $exception->getMessage();
|
|
$this->code = $exception->getCode();
|
|
$this->trace = $exception->getTraceAsString();
|
|
}
|
|
|
|
public function promise(): Promise {
|
|
switch ($this->parent) {
|
|
case self::PARENT_ERROR:
|
|
$exception = new TaskError(
|
|
$this->type,
|
|
sprintf(
|
|
'Uncaught %s in worker with message "%s" and code "%s"',
|
|
$this->type,
|
|
$this->message,
|
|
$this->code
|
|
),
|
|
$this->trace
|
|
);
|
|
break;
|
|
|
|
default:
|
|
$exception = new TaskException(
|
|
$this->type,
|
|
sprintf(
|
|
'Uncaught %s in worker with message "%s" and code "%s"',
|
|
$this->type,
|
|
$this->message,
|
|
$this->code
|
|
),
|
|
$this->trace
|
|
);
|
|
}
|
|
|
|
return new Failure($exception);
|
|
}
|
|
}
|