How to test response to failed notification?
I want to be able to alert my application admins if a notification fails to send. Typically this happens using Postmark when the email address is marked by Postmark as inactive and this exception is thrown
Symfony\Component\Mailer\Exception\HttpTransportException: Unable to send an email: You tried to send to a recipient that has been marked as inactive.
I've been trying to do it by adding the Trait below to the notification to provide a common failed() method which sends a different FailedNotification to the admins. I also use it to set a common via method so that I can pass the failed notifiable (User) to to the notification that will go to the admin.
namespace App\Traits\User;
use App\Models\User;
use App\Notifications\Admin\FailedNotification;
use Throwable;
trait NotificationFailed
{
public User $notifiable;
public function via(User $user) : array
{
$this->notifiable = $user;
return ['mail'];
}
public function failed(Throwable $exception) : void
{
User::role('administrator')
->get()
->each(fn ($user) => $user->notify(new FailedNotification($exception, $this->notifiable)));
}
}
The HttpTransportException doesn't get caught and processed as far as I can see. I'd like to know why so I've been scratching my head about how to test it properly (which would be a good thing anyway).
I can't find a way to set things up so that Notification::fake() in my test results in the HttpTransportException being thrown so I can see what happens.
I'd be grateful for any suggestions :)
Please or to participate in this conversation.