I noticed today that some billing-related jobs in HYVOR production failed, but these were not reported to Sentry, which was extremely concerning (thankfully, they were logged in the failed_jobs
table). Turns out, due to how Laravel handles queues, exceptions are not thrown outside of the job. Sentry recommends manually reporting them to Sentry in the job’s failed
method. However, I am not going to duplicate the same code in all jobs. What happens when we forget one someday?
Listening to Failed Job events and sending the error to Sentry is much better.
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Support\Facades\Queue; 6use Illuminate\Support\ServiceProvider; 7use Illuminate\Queue\Events\JobFailed; 8 9class AppServiceProvider extends ServiceProvider10{11 public function boot(): void12 {13 Queue::failing(function (JobFailed $event) {14 if (app()->bound('sentry')) {15 app('sentry')->captureException($event->exception);16 }17 });18 }19}
Comments