2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Worker\Internal;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
use Amp\Failure;
|
2017-05-18 06:13:29 +02:00
|
|
|
use Amp\Parallel\Worker\TaskError;
|
|
|
|
use Amp\Parallel\Worker\TaskException;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Promise;
|
2016-08-21 17:33:39 +02:00
|
|
|
|
2017-07-28 06:49:20 +02:00
|
|
|
/** @internal */
|
2018-10-21 17:54:46 +02:00
|
|
|
final class TaskFailure extends TaskResult
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-05-18 06:13:29 +02:00
|
|
|
const PARENT_EXCEPTION = 0;
|
|
|
|
const PARENT_ERROR = 1;
|
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
/** @var string */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $type;
|
|
|
|
|
2017-05-18 06:13:29 +02:00
|
|
|
/** @var int */
|
|
|
|
private $parent;
|
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
/** @var string */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $message;
|
|
|
|
|
2017-11-10 18:32:15 +01:00
|
|
|
/** @var int|string */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $code;
|
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
/** @var array */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $trace;
|
|
|
|
|
2019-01-25 21:29:32 +01:00
|
|
|
/** @var self|null */
|
|
|
|
private $previous;
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function __construct(string $id, \Throwable $exception)
|
|
|
|
{
|
2016-09-07 18:38:46 +02:00
|
|
|
parent::__construct($id);
|
2016-09-02 01:10:52 +02:00
|
|
|
$this->type = \get_class($exception);
|
2017-05-18 06:13:29 +02:00
|
|
|
$this->parent = $exception instanceof \Error ? self::PARENT_ERROR : self::PARENT_EXCEPTION;
|
2015-08-27 16:10:08 +02:00
|
|
|
$this->message = $exception->getMessage();
|
|
|
|
$this->code = $exception->getCode();
|
|
|
|
$this->trace = $exception->getTraceAsString();
|
2019-01-25 21:29:32 +01:00
|
|
|
|
|
|
|
if ($previous = $exception->getPrevious()) {
|
|
|
|
$this->previous = new self($id, $previous);
|
|
|
|
}
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function promise(): Promise
|
|
|
|
{
|
2019-01-25 21:29:32 +01:00
|
|
|
return new Failure($this->createException());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createException(): \Throwable
|
|
|
|
{
|
|
|
|
$previous = $this->previous ? $this->previous->createException() : null;
|
2017-05-18 06:13:29 +02:00
|
|
|
|
2019-01-25 21:29:32 +01:00
|
|
|
if ($this->parent === self::PARENT_ERROR) {
|
|
|
|
return new TaskError(
|
|
|
|
$this->type,
|
|
|
|
\sprintf(
|
|
|
|
'Uncaught %s in worker with message "%s" and code "%s"',
|
2017-05-18 06:13:29 +02:00
|
|
|
$this->type,
|
2019-01-25 21:29:32 +01:00
|
|
|
$this->message,
|
|
|
|
$this->code
|
|
|
|
),
|
|
|
|
$this->trace,
|
|
|
|
$previous
|
|
|
|
);
|
2017-05-18 06:13:29 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 21:29:32 +01:00
|
|
|
return new TaskException(
|
|
|
|
$this->type,
|
|
|
|
\sprintf(
|
|
|
|
'Uncaught %s in worker with message "%s" and code "%s"',
|
|
|
|
$this->type,
|
|
|
|
$this->message,
|
|
|
|
$this->code
|
|
|
|
),
|
|
|
|
$this->trace,
|
|
|
|
$previous
|
|
|
|
);
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
}
|