0

Using Laravel 5.1's Queues, I'm throwing an exception when a job fails.

throw new \Exception('No luck');

As Laravel recommends when dealing with failed jobs, I'm "catching" the exception in the AppServiceProvider, and using that to send our team an email.

public function boot()
{
    Queue::failing(function ($connection, $job, $data) {
        $info['data'] = $data;
        \Mail::send('emails.jobs.failed', $info, function($message) {
            $message->to('test@email.com')->subject('Job failed');
        });
    });
}

Within the email, I would like to place the exception's message (in this case "No luck."). But I can't figure out how to pass that along to Queue::failing().

Any ideas?

Marty Thomas
  • 857
  • 2
  • 9
  • 18

1 Answers1

1

After calling the failing callback, Laravel rethrows the exception.

It seems if you really need the error message, you'll have to catch the exception yourself.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Thanks Joseph. I'm really trying to figure out a why to let our team know why a job failed in the first place. Right now, were only being notified that a specific job fails, with no details why. Is there a better way to do that? – Marty Thomas Nov 17 '15 at 19:45
  • @MartyThomas - I'm not sure what you're asking. Like I said in the answer, you can catch the exception and send the email there. – Joseph Silber Nov 17 '15 at 19:56
  • @MartyThomas - the reason you're running into this is because the failures were never meant to be used the way you are. A queue job shouldn't use exceptions to control app flow. If for whatever reason you couldn't execute the job, you should handle that within the job's handler. The `failing` callback is meant for unexpected failures (the exception is still logged to the main app log file). – Joseph Silber Nov 17 '15 at 19:58
  • I was using the exception in the question, because I thought that was a good way to demonstrate what I was trying to do, but that's obviously not working too well :) I was having trouble debugging why jobs were failing (unexpectedly), and I thought that the `failing` callback would be the solution. But maybe I'm thinking about this in the wrong way. Thanks again for the thoughts. – Marty Thomas Nov 17 '15 at 21:07
  • It is [still the case](https://github.com/laravel/framework/commit/3adc11fa6a41591d267cab5bf0c3ea2d388f3185/src/Illuminate/Queue/Worker.php#L378) after like 4 years. Seems the correct way btw. – ShahinSorkh Jul 01 '19 at 14:08