4

I have Sentry.io set-up on my Laravel project. I'm also using Queues.

I was wondering if it was possible to send failed queues to Sentry? As they don't automatically send when they fail.

2 Answers2

10

By failed queues I guess you mean failed jobs, for that you just need to implement the failed() method inside the job:

/**
 * Handle a job failure.
 *
 * @return void
 */
public function failed(\Exception $exception)
{
    // Send exception data to sentry.io
    // It should catch it by default since it throws an exception
    // But you can force a report manually
    app('sentry')->captureException($exception);
}

Check how to deal with failed jobs in Laravel documentation.

Asur
  • 3,727
  • 1
  • 26
  • 34
  • 1
    Just adding more context, this is also specified in Sentry documentation (Since the answer is from 2018, I also want to mention that when I am adding this comment we are on Laravel 10): https://docs.sentry.io/platforms/php/guides/laravel/usage/#queue-jobs – Eduardo Cruz May 23 '23 at 20:31
2

If you added the following snippet to your error handler (as described here), all uncatched exceptions (also when thrown in queue jobs) are catched as long as they pass the ->shouldReport() check.

if (app()->bound('sentry') && $this->shouldReport($exception)) {
    app('sentry')->captureException($exception);
}
Alex
  • 1,425
  • 11
  • 25