I am having a weird issue while throwing the exception inside my beanstalkd job class. When I throw a simple exception (throw new \Exception();
) outside the job class, it calls the report() method in App\Exceptions\Handler and I can do my tweaks there. But when I throw the exception inside the job class, it does not call the report() method. All the queue implementation is based on laravel documentation(https://laravel.com/docs/5.1/queues) and works great. What can be the reason for this? Any help would be highly appreciated.
Asked
Active
Viewed 6,250 times
6

Tibin Paul
- 806
- 9
- 23
2 Answers
8
Well, it had to be different for queues. If a job fails (throws an exception), you don't want to give the control to the Exception handler – you want to catch the error and mark the job as failed.
Look inside Worker.php (line 294+):
protected function raiseExceptionOccurredJobEvent($connection, Job $job, $exception)
{
if ($this->events) {
$data = json_decode($job->getRawBody(), true);
$this->events->fire(new Events\JobExceptionOccurred($connection, $job, $data, $exception));
}
}
See process()
on lines 201-223 as well.
Laravel will catch the exception but will fire an Event to let you know there has been a problem. If you need to hook into it – just listen for this event.

Denis Mysenko
- 6,366
- 1
- 24
- 33
-
1Thanks @Denis Mysenko. I agree. Listening to the event seems to be a better idea which makes the code clean and handy. – Tibin Paul Jun 08 '16 at 11:01
-
i have another Worker.php in my Laravel 5.1. What version do you use? – Yevgeniy Afanasyev Jan 24 '17 at 09:35
-
In laravel 5.3 I have found this function. Thank you. – Yevgeniy Afanasyev Jan 25 '17 at 00:31
0
With laravel 5.1 I had the same problem when I was using QUEUE_DRIVER=sync
when I changed it to QUEUE_DRIVER=redis
and exception handler started to do it's job, I mean stardted to call the report()
method.
Be sure you use run start artisan queue:work redis
with --daemon

Yevgeniy Afanasyev
- 37,872
- 26
- 173
- 191