From 88043a222f9a4e604e0a62bd87ad7e888dee261d Mon Sep 17 00:00:00 2001 From: Michiel Kempen Date: Thu, 17 Sep 2020 10:20:25 +0200 Subject: [PATCH] use ParallelException instead of Exception as fallback --- src/Output/ParallelException.php | 31 +++++++++++++++ src/Output/SerializableException.php | 5 +-- tests/ErrorHandlingTest.php | 38 +++++++++++++++---- ...hp => MyExceptionWithAComplexArgument.php} | 2 +- .../MyExceptionWithAComplexFirstArgument.php | 17 +++++++++ 5 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 src/Output/ParallelException.php rename tests/{MyComplexException.php => MyExceptionWithAComplexArgument.php} (81%) create mode 100644 tests/MyExceptionWithAComplexFirstArgument.php diff --git a/src/Output/ParallelException.php b/src/Output/ParallelException.php new file mode 100644 index 00000000..ddd43a13 --- /dev/null +++ b/src/Output/ParallelException.php @@ -0,0 +1,31 @@ +originalClass = $originalClass; + $this->originalTrace = $originalTrace; + } + + /** @return string */ + public function getOriginalClass(): string + { + return $this->originalClass; + } + + /** @return string */ + public function getOriginalTrace(): string + { + return $this->originalTrace; + } +} diff --git a/src/Output/SerializableException.php b/src/Output/SerializableException.php index 8d145ae8..f8e97a83 100644 --- a/src/Output/SerializableException.php +++ b/src/Output/SerializableException.php @@ -2,7 +2,6 @@ namespace Spatie\Async\Output; -use ArgumentCountError; use Throwable; class SerializableException @@ -28,8 +27,8 @@ public function asThrowable(): Throwable try { /** @var Throwable $throwable */ $throwable = new $this->class($this->message."\n\n".$this->trace); - } catch (ArgumentCountError $exception) { - $throwable = new \Exception($this->message."\n\n".$this->trace); + } catch (Throwable $exception) { + $throwable = new ParallelException($this->message, $this->class, $this->trace); } return $throwable; diff --git a/tests/ErrorHandlingTest.php b/tests/ErrorHandlingTest.php index fe2fbcad..b3c47e1b 100644 --- a/tests/ErrorHandlingTest.php +++ b/tests/ErrorHandlingTest.php @@ -7,6 +7,7 @@ use ParseError; use PHPUnit\Framework\TestCase; use Spatie\Async\Output\ParallelError; +use Spatie\Async\Output\ParallelException; use Spatie\Async\Pool; class ErrorHandlingTest extends TestCase @@ -34,17 +35,40 @@ public function it_can_handle_complex_exceptions_via_catch_callback() { $pool = Pool::create(); - foreach (range(1, 5) as $i) { - $pool->add(function () { - throw new MyComplexException('test', (object) ['error' => 'wrong query']); - })->catch(function (MyComplexException $e) { - $this->assertRegExp('/test/', $e->getMessage()); + $originalExceptionCount = 0; + $fallbackExceptionCount = 0; + + $pool + ->add(function () { + throw new MyExceptionWithAComplexArgument('test', (object) ['error' => 'wrong query']); + }) + ->catch(function (MyExceptionWithAComplexArgument $e) use (&$originalExceptionCount) { + $originalExceptionCount += 1; + }) + ->catch(function (ParallelException $e) use (&$fallbackExceptionCount) { + $fallbackExceptionCount += 1; + $this->assertEquals('test', $e->getMessage()); + $this->assertEquals(MyExceptionWithAComplexArgument::class, $e->getOriginalClass()); + }); + + $pool + ->add(function () use (&$originalExceptionCount) { + throw new MyExceptionWithAComplexFirstArgument((object) ['error' => 'wrong query'], 'test'); + }) + ->catch(function (MyExceptionWithAComplexFirstArgument $e) use (&$originalExceptionCount) { + $originalExceptionCount += 1; + }) + ->catch(function (ParallelException $e) use (&$fallbackExceptionCount) { + $fallbackExceptionCount += 1; + $this->assertEquals('test', $e->getMessage()); + $this->assertEquals(MyExceptionWithAComplexFirstArgument::class, $e->getOriginalClass()); }); - } $pool->wait(); - $this->assertCount(5, $pool->getFailed(), (string) $pool->status()); + $this->assertCount(2, $pool->getFailed(), (string) $pool->status()); + $this->assertEquals(0, $originalExceptionCount); + $this->assertEquals(2, $fallbackExceptionCount); } /** @test */ diff --git a/tests/MyComplexException.php b/tests/MyExceptionWithAComplexArgument.php similarity index 81% rename from tests/MyComplexException.php rename to tests/MyExceptionWithAComplexArgument.php index 476b2cca..43ec93c6 100644 --- a/tests/MyComplexException.php +++ b/tests/MyExceptionWithAComplexArgument.php @@ -5,7 +5,7 @@ use Exception; use stdClass; -class MyComplexException extends Exception +class MyExceptionWithAComplexArgument extends Exception { public $payload; diff --git a/tests/MyExceptionWithAComplexFirstArgument.php b/tests/MyExceptionWithAComplexFirstArgument.php new file mode 100644 index 00000000..8ca66f77 --- /dev/null +++ b/tests/MyExceptionWithAComplexFirstArgument.php @@ -0,0 +1,17 @@ +payload = $payload; + } +}