Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Toufik94's avatar

How to handle exceptions to the user when sending queued notifications?

Hi guys,

Im sending a simple queued notification to my users whenever they try to login. They will receive an email containing a verification code. This all works well, no problems at all.

However, I can't seem to get my head around how to notify users when the job fails. I tried using a try-catch block but that did nothing:

$user = auth()->user();
$user->generateTwoFactorCode();
 try {
				$user->notify(new TwoFactorCode());
                 return redirect()->back()->withStatus("Good, it worked!");
 } catch (\Exception $e) {
                return redirect()->back()->withErrors(["two_factor_code" => "Whoopsie, didnt work!."]);
        }

It never catches an exception even though some of these jobs end up in the failed_jobs table.

I also tried using the ''failed' method in the Notification class and Im able to catch the exceptions there, but I cant find a way to send that info to the frontend, which is Blade.

0 likes
20 replies
tykus's avatar

@Toufik94 ok, can I assume you're running a queue worker and the jobs are being handled asynchronously; but you also want to handle any Exception synchronously so the User gets feedback???

Toufik94's avatar

@tykus Your assumption is correct. Im running php artisan queue:work in the background and my mail job is being handled asynchronously. It works perfectly fine. I just don't know how to handle a potential failed job in way that my user understands something is wrong. The Laravel docs are only talking about this ''failed'' method thats being fired when a jobs fails. But it doesnt explain how you can use that method to notify the user in the frontend.

Or is that not a thing with failed jobs?

tykus's avatar

@Toufik94 the queue worker is a separate process; so the execution of the Job / queued Notification will not be in the same context as the Request that dispatched it. Therefore, you cannot catch/handle an Exception thrown by the execution of the Job itself!

Toufik94's avatar

@tykus So in other words, there is no way to alert the user that his queuable notification didnt went through? Only if its sent synchronously.

tykus's avatar

@Toufik94 you can, within the context of the Request/Response lifecycle, say that it was dispatched successfully.

But you cannot say that it was handled successfully unless you do something within the failed method of the Notification class. What you do there could be anything... a separate notification, an email, a broadcast message...

Toufik94's avatar

@tykus The most important thing for me is that the user isnt waiting for an unlimited time questioning himself why his e-mail hasnt come through yet while ealier he recieved an alert that the email will be sent to him (see my controller method in my opening post) after hitting ''send''.

I just need a way to alert him that something has gone wrong if the notification didnt get through due to failure at the side of the queue/job. From what Im understanding from your reaction is that you can do all sort of stuff within the failed-method of the Notification class but not like flash a new message to the request where that email request came from...right?

Toufik94's avatar

@MohamedTammam That section only talks about manually releasing or failing a job. It doesn't specifically give info about handling errors for the user to be seen.

Toufik94's avatar

@MohamedTammam Yeah, Ive seen that part. It talks about the failed method inside the Jobs class. I tried that out and Im able to catch the errors. But there is nothing I can do with that error as in send it to the user to alert him in the frontend. Its because the process of executing the jobs isnt part of the request.

tykus's avatar

@Toufik94 you can send a Notification to the User that dispatched the original queued notification.

Toufik94's avatar

@tykus Like a new e-mail? And what if that Notification also isnt coming through due to some structural error causing the jobs to constantly fail? Will the user then keep getting a new e-mail telling him that something went wrong?

I just want the user to see some sort of error message in the frontend like: ''Uh oh, it looks like something went wrong on our side. If this error keeps showing up, please contact our tech team [email protected]".

Or is not a good way to alert a user when jobs fail? Im new to this topic, so excuse me.

krisi_gjika's avatar

@Toufik94 you can broadcast a message to the user, when the job fails from the failed method. But you cannot return a message to the blade view, nor write a message to the session as the job is not running as part of the user request. The user request has finished when you dispatched the job and is no longer waiting for a response. You need another process that listens on the front end for these messages separate from the requests the user submits OR resolve the job on the same process as a synchronous process.

Toufik94's avatar

@krisi_gjika

You need another process that listens on the front end for these messages separate from the requests the user submits

So, something like a event listener that checks on failed jobs? Or a specific job.

Please or to participate in this conversation.