diff --git a/lib/Forking/Fork.php b/lib/Forking/Fork.php index 7085abb..bc554ce 100644 --- a/lib/Forking/Fork.php +++ b/lib/Forking/Fork.php @@ -2,19 +2,11 @@ namespace Amp\Parallel\Forking; -use function Amp\call; use Amp\{ Coroutine, Loop, Promise }; -use Amp\Parallel\{ - ContextException, - ChannelException, - Process, - SerializationException, - StatusError, - Strand, - SynchronizationError -}; -use Amp\Parallel\Sync\{ Channel, ChannelledSocket }; +use Amp\Parallel\{ ContextException, Process, StatusError, Strand, SynchronizationError }; +use Amp\Parallel\Sync\{ Channel, ChannelException, ChannelledSocket, SerializationException }; use Amp\Parallel\Sync\Internal\{ ExitFailure, ExitResult, ExitSuccess }; +use function Amp\call; /** * Implements a UNIX-compatible context using forked processes. diff --git a/lib/Process/ChannelledProcess.php b/lib/Process/ChannelledProcess.php index e4f3c6e..afa4516 100644 --- a/lib/Process/ChannelledProcess.php +++ b/lib/Process/ChannelledProcess.php @@ -5,14 +5,13 @@ namespace Amp\Parallel\Process; use function Amp\call; use Amp\{ Coroutine, Promise }; use Amp\Parallel\{ - ChannelException, ContextException, Process as ProcessContext, StatusError, Strand, SynchronizationError }; -use Amp\Parallel\Sync\{ ChannelledSocket, Internal\ExitResult }; +use Amp\Parallel\Sync\{ ChannelException, ChannelledSocket, Internal\ExitResult }; use Amp\Process\Process; class ChannelledProcess implements ProcessContext, Strand { diff --git a/lib/ChannelException.php b/lib/Sync/ChannelException.php similarity index 86% rename from lib/ChannelException.php rename to lib/Sync/ChannelException.php index c4f0f35..0c61d48 100644 --- a/lib/ChannelException.php +++ b/lib/Sync/ChannelException.php @@ -1,6 +1,6 @@ jobQueue)) { diff --git a/lib/Worker/Internal/TaskFailure.php b/lib/Worker/Internal/TaskFailure.php index 4fb0770..3ad381b 100644 --- a/lib/Worker/Internal/TaskFailure.php +++ b/lib/Worker/Internal/TaskFailure.php @@ -3,13 +3,20 @@ namespace Amp\Parallel\Worker\Internal; use Amp\Failure; -use Amp\Parallel\TaskException; +use Amp\Parallel\Worker\TaskError; +use Amp\Parallel\Worker\TaskException; use Amp\Promise; class TaskFailure extends TaskResult { + const PARENT_EXCEPTION = 0; + const PARENT_ERROR = 1; + /** @var string */ private $type; + /** @var int */ + private $parent; + /** @var string */ private $message; @@ -22,17 +29,32 @@ class TaskFailure extends TaskResult { 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 { - return new Failure(new TaskException( - $this->type, - sprintf('Uncaught exception in worker of type "%s" with message "%s"', $this->type, $this->message), - $this->code, - $this->trace - )); + switch ($this->parent) { + case self::PARENT_ERROR: + $exception = new TaskError( + $this->type, + sprintf('Uncaught Error in worker of type "%s" with message "%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"', $this->type, $this->message), + $this->code, + $this->trace + ); + } + + return new Failure($exception); } } \ No newline at end of file diff --git a/lib/Worker/TaskError.php b/lib/Worker/TaskError.php new file mode 100644 index 0000000..14d4fdb --- /dev/null +++ b/lib/Worker/TaskError.php @@ -0,0 +1,41 @@ +name = $name; + $this->trace = $trace; + } + + /** + * Returns the class name of the error thrown from the task. + * + * @return string + */ + public function getName(): string { + return $this->name; + } + + /** + * Gets the stack trace at the point the error was thrown in the task. + * + * @return string + */ + public function getWorkerTrace(): string { + return $this->trace; + } +} diff --git a/lib/TaskException.php b/lib/Worker/TaskException.php similarity index 94% rename from lib/TaskException.php rename to lib/Worker/TaskException.php index 2d7a223..75fe7d2 100644 --- a/lib/TaskException.php +++ b/lib/Worker/TaskException.php @@ -1,6 +1,6 @@ createCallback(1)); diff --git a/test/Sync/SharedMemoryParcelTest.php b/test/Sync/SharedMemoryParcelTest.php index 3125d15..744962a 100644 --- a/test/Sync/SharedMemoryParcelTest.php +++ b/test/Sync/SharedMemoryParcelTest.php @@ -35,7 +35,7 @@ class SharedMemoryParcelTest extends AbstractParcelTest { } /** - * @expectedException \Amp\Parallel\SharedMemoryException + * @expectedException \Amp\Parallel\Sync\SharedMemoryException */ public function testUnwrapThrowsErrorIfFreed() { $object = new SharedMemoryParcel(new \stdClass());