1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/lib/Worker/Internal/TaskFailure.php

70 lines
1.9 KiB
PHP
Raw Normal View History

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
use Amp\Failure;
use Amp\Parallel\Worker\TaskError;
use Amp\Parallel\Worker\TaskException;
use Amp\Promise;
2017-07-28 06:49:20 +02:00
/** @internal */
2016-09-07 18:38:46 +02:00
class TaskFailure extends TaskResult {
const PARENT_EXCEPTION = 0;
const PARENT_ERROR = 1;
/** @var string */
2015-08-27 16:10:08 +02:00
private $type;
/** @var int */
private $parent;
/** @var string */
2015-08-27 16:10:08 +02:00
private $message;
/** @var int|string */
2015-08-27 16:10:08 +02:00
private $code;
/** @var array */
2015-08-27 16:10:08 +02:00
private $trace;
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);
$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();
}
2017-05-18 09:51:31 +02:00
2016-11-15 00:43:44 +01:00
public function promise(): Promise {
switch ($this->parent) {
case self::PARENT_ERROR:
$exception = new TaskError(
$this->type,
sprintf(
'Uncaught Error in worker of type "%s" with message "%s" and code "%s"',
$this->type,
$this->message,
$this->code
),
$this->trace
);
break;
default:
$exception = new TaskException(
$this->type,
sprintf(
'Uncaught Exception in worker of type "%s" with message "%s" and code "%s"',
$this->type,
$this->message,
$this->code
),
$this->trace
);
}
return new Failure($exception);
2015-08-27 16:10:08 +02:00
}
2017-05-18 09:51:31 +02:00
}