Context: I have an export that I want to mail to its intended recipients once it is stored.
I do this inside a Job class.
public function handle(): void
{
(new ReportExport($this->report, $this->date))->queue($this->report->path)
->chain([
new NotifyReportRecipients($this->report),
]);
}
Inside the export, I'm using the FromGenerator concern.
public function generator(): Generator
{
$query = DB::query()->select(...)->from(...)->where(...);
return $query->cursor()->getIterator();
}
This already works.
Now, I want to send a different notification but only in the case the export is empty, so I'm trying to cause an exception so I can handle it in the Job class.
use Path\To\MyException;
public function generator(): Generator
{
$query = DB::query()->select(...)->from(...)->where(...);
throw_if($query->count() === 0, MyException::class);
return $query->cursor()->getIterator();
}
But it does not work since the Laravel-excel developers didn't implement a catch method for their QueuedExport class.
public function handle(): void
{
(new ReportExport($this->report, $this->date))->queue($this->report->path)
->chain([
new NotifyReportRecipients($this->report),
])
->catch(function (MyException $e) {
new NotifyReportRecipientsAboutEmptyReport($this->report),
});
// Error Call to undefined method Maatwebsite\Excel\Jobs\QueueExport::catch()
}
I've tried using a try...catch block
public function handle(): void
{
try {
(new ReportExport($this->report, $this->date))->queue($this->report->path)
->chain([
new NotifyReportRecipients($this->report),
]);
} catch (MyException $e) {
NotifyReportRecipientsAboutEmptyReport::dispatch($this->report);
}
}
But it does not work.
I've tried following the documentation for Laravel excel and just add a failed method to my Export class but this doesn't work either.
use Path\To\MyException;
public function failed(Throwable $e)
{
if ($e instanceof MyException) {
NotifyReportRecipientsAboutEmptyReport::dispatch($this->report);
}
}
public function generator(): Generator
{
$query = DB::query()->select(...)->from(...)->where(...);
throw_if($query->count() === 0, MyException::class);
return $query->cursor()->getIterator();
}
What's worse, is that the chain just dispatches the Notification regardless.
Does anyone know the correct way to deal with this?
composer show, I have the 3.1.51 version installed.