From 5f9e7d5754f9a65def2e916a2ff1b70a7b1664fc Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Tue, 17 Sep 2019 19:11:02 -0400 Subject: [PATCH] Catch error messages in notifying (#403) * Catch error messages in notifying * Formatting --- app/Listeners/NotificationEvents.php | 32 ++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/app/Listeners/NotificationEvents.php b/app/Listeners/NotificationEvents.php index f15ea481..192d87de 100644 --- a/app/Listeners/NotificationEvents.php +++ b/app/Listeners/NotificationEvents.php @@ -40,7 +40,25 @@ class NotificationEvents extends Listener protected function notifyAdmins($notification) { $admin_users = User::whereRoleIs('admin')->get(); - Notification::send($admin_users, $notification); + + try { + Notification::send($admin_users, $notification); + } catch (\Exception $e) { + Log::emergency('Error emailing admins, malformed email='.$e->getMessage()); + } + } + + /** + * @param User $user + * @param \Illuminate\Notifications\Notification $notification + */ + protected function notifyUser($user, $notification) + { + try { + $user->notify($notification); + } catch (\Exception $e) { + Log::emergency('Error emailing admins, malformed email='.$e->getMessage()); + } } /** @@ -64,9 +82,9 @@ class NotificationEvents extends Listener * Send the user a confirmation email */ if ($event->user->state === UserState::ACTIVE) { - $event->user->notify(new \App\Notifications\UserRegistered($event->user)); + $this->notifyUser($event->user, new \App\Notifications\UserRegistered($event->user)); } elseif ($event->user->state === UserState::PENDING) { - $event->user->notify(new \App\Notifications\UserPending($event->user)); + $this->notifyUser($event->user, new \App\Notifications\UserPending($event->user)); } } @@ -81,9 +99,9 @@ class NotificationEvents extends Listener if ($event->old_state === UserState::PENDING) { if ($event->user->state === UserState::ACTIVE) { - $event->user->notify(new \App\Notifications\UserRegistered($event->user)); + $this->notifyUser($event->user, new \App\Notifications\UserRegistered($event->user)); } elseif ($event->user->state === UserState::REJECTED) { - $event->user->notify(new \App\Notifications\UserRejected($event->user)); + $this->notifyUser($event->user, new \App\Notifications\UserRejected($event->user)); } } elseif ($event->old_state === UserState::ACTIVE) { Log::info('User state change from active to ??'); @@ -109,7 +127,7 @@ class NotificationEvents extends Listener public function onPirepAccepted(PirepAccepted $event): void { Log::info('NotificationEvents::onPirepAccepted: '.$event->pirep->id.' accepted'); - $event->pirep->user->notify(new \App\Notifications\PirepAccepted($event->pirep)); + $this->notifyUser($event->pirep->user, new \App\Notifications\PirepAccepted($event->pirep)); } /** @@ -120,6 +138,6 @@ class NotificationEvents extends Listener public function onPirepRejected(PirepRejected $event): void { Log::info('NotificationEvents::onPirepRejected: '.$event->pirep->id.' rejected'); - $event->pirep->user->notify(new \App\Notifications\PirepRejected($event->pirep)); + $this->notifyUser($event->pirep->user, new \App\Notifications\PirepRejected($event->pirep)); } }