mirror of
https://github.com/archtechx/jobpipeline.git
synced 2025-12-12 06:44:03 +00:00
fix: handle errors in closures (#17)
* fix: handle errors in closures * test: add test
This commit is contained in:
parent
8b793e0cf2
commit
ebaa3fb317
2 changed files with 18 additions and 1 deletions
|
|
@ -72,7 +72,7 @@ class JobPipeline implements ShouldQueue
|
||||||
try {
|
try {
|
||||||
$result = app()->call($job);
|
$result = app()->call($job);
|
||||||
} catch (Throwable $exception) {
|
} catch (Throwable $exception) {
|
||||||
if (method_exists(get_class($job[0]), 'failed')) {
|
if (is_array($job) && method_exists(get_class($job[0]), 'failed')) {
|
||||||
call_user_func_array([$job[0], 'failed'], [$exception]);
|
call_user_func_array([$job[0], 'failed'], [$exception]);
|
||||||
} else {
|
} else {
|
||||||
throw $exception;
|
throw $exception;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Stancl\JobPipeline\Tests;
|
namespace Stancl\JobPipeline\Tests;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Queue;
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
|
@ -193,6 +194,22 @@ class JobPipelineTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue($passes);
|
$this->assertTrue($passes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function failures_in_closures_will_throw_correctly()
|
||||||
|
{
|
||||||
|
$this->expectExceptionMessage('foobar');
|
||||||
|
|
||||||
|
Event::listen(TestEvent::class, JobPipeline::make([
|
||||||
|
function () {
|
||||||
|
throw new Exception('foobar');
|
||||||
|
}
|
||||||
|
])->send(function (TestEvent $event) {
|
||||||
|
return $this->valuestore;
|
||||||
|
})->shouldBeQueued(false)->toListener());
|
||||||
|
|
||||||
|
event(new TestEvent(new TestModel()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FooJob
|
class FooJob
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue