Skip to content

Commit

Permalink
use ParallelException instead of Exception as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
michielkempen committed Sep 17, 2020
1 parent db20e96 commit 88043a2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
31 changes: 31 additions & 0 deletions src/Output/ParallelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\Async\Output;

class ParallelException extends \Exception
{
/** @var string */
protected $originalClass;

/** @var string */
protected $originalTrace;

public function __construct(string $message, string $originalClass, string $originalTrace)
{
parent::__construct($message);
$this->originalClass = $originalClass;
$this->originalTrace = $originalTrace;
}

/** @return string */
public function getOriginalClass(): string
{
return $this->originalClass;
}

/** @return string */
public function getOriginalTrace(): string
{
return $this->originalTrace;
}
}
5 changes: 2 additions & 3 deletions src/Output/SerializableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Spatie\Async\Output;

use ArgumentCountError;
use Throwable;

class SerializableException
Expand All @@ -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;
Expand Down
38 changes: 31 additions & 7 deletions tests/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use stdClass;

class MyComplexException extends Exception
class MyExceptionWithAComplexArgument extends Exception
{
public $payload;

Expand Down
17 changes: 17 additions & 0 deletions tests/MyExceptionWithAComplexFirstArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\Async\Tests;

use Exception;
use stdClass;

class MyExceptionWithAComplexFirstArgument extends Exception
{
public $payload;

public function __construct(stdClass $payload, string $message)
{
parent::__construct($message);
$this->payload = $payload;
}
}

0 comments on commit 88043a2

Please sign in to comment.