Merge pull request #16050 from Godmartinz/inactive-slack-hook
Some checks failed
Crowdin Action / upload-sources-to-crowdin (push) Has been cancelled
Docker images (Alpine) / docker (push) Has been cancelled
Docker images / docker (push) Has been cancelled
Tests in MySQL / PHP ${{ matrix.php-version }} (8.1) (push) Has been cancelled
Tests in MySQL / PHP ${{ matrix.php-version }} (8.2) (push) Has been cancelled
Tests in MySQL / PHP ${{ matrix.php-version }} (8.3) (push) Has been cancelled
Tests in SQLite / PHP ${{ matrix.php-version }} (8.1.1) (push) Has been cancelled

Adds try/catch around notification failing with an inactive webhook for better user experience
This commit is contained in:
snipe 2025-01-10 11:23:41 +00:00 committed by GitHub
commit 224f04db6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 33 deletions

View file

@ -46,7 +46,7 @@ class CheckoutableListener
*/ */
public function onCheckedOut($event) public function onCheckedOut($event)
{ {
if ($this->shouldNotSendAnyNotifications($event->checkoutable)){ if ($this->shouldNotSendAnyNotifications($event->checkoutable)) {
return; return;
} }
@ -57,7 +57,7 @@ class CheckoutableListener
$acceptance = $this->getCheckoutAcceptance($event); $acceptance = $this->getCheckoutAcceptance($event);
$adminCcEmailsArray = []; $adminCcEmailsArray = [];
if($settings->admin_cc_email !== '') { if ($settings->admin_cc_email !== '') {
$adminCcEmail = $settings->admin_cc_email; $adminCcEmail = $settings->admin_cc_email;
$adminCcEmailsArray = array_map('trim', explode(',', $adminCcEmail)); $adminCcEmailsArray = array_map('trim', explode(',', $adminCcEmail));
} }
@ -65,7 +65,7 @@ class CheckoutableListener
$mailable = $this->getCheckoutMailType($event, $acceptance); $mailable = $this->getCheckoutMailType($event, $acceptance);
$notifiable = $this->getNotifiables($event); $notifiable = $this->getNotifiables($event);
if ($event->checkedOutTo->locale){ if ($event->checkedOutTo->locale) {
$mailable->locale($event->checkedOutTo->locale); $mailable->locale($event->checkedOutTo->locale);
} }
// Send email notifications // Send email notifications
@ -77,41 +77,50 @@ class CheckoutableListener
* 3. The item should send an email at check-in/check-out * 3. The item should send an email at check-in/check-out
*/ */
if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() || if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() ||
$this->checkoutableShouldSendEmail($event)) { $this->checkoutableShouldSendEmail($event)) {
Log::info('Sending checkout email, Locale: ' . ($event->checkedOutTo->locale ?? 'default')); Log::info('Sending checkout email, Locale: ' . ($event->checkedOutTo->locale ?? 'default'));
if (!empty($notifiable)) { if (!empty($notifiable)) {
Mail::to($notifiable)->cc($ccEmails)->send($mailable); Mail::to($notifiable)->cc($ccEmails)->send($mailable);
} elseif (!empty($ccEmails)) { } elseif (!empty($ccEmails)) {
Mail::cc($ccEmails)->send($mailable); Mail::cc($ccEmails)->send($mailable);
}
Log::info('Checkout Mail sent.');
} }
Log::info('Checkout Mail sent.');
}
} catch (ClientException $e) { } catch (ClientException $e) {
Log::debug("Exception caught during checkout email: " . $e->getMessage()); Log::debug("Exception caught during checkout email: " . $e->getMessage());
} catch (Exception $e) { } catch (Exception $e) {
Log::debug("Exception caught during checkout email: " . $e->getMessage()); Log::debug("Exception caught during checkout email: " . $e->getMessage());
} }
// Send Webhook notification // Send Webhook notification
try{ try {
if ($this->shouldSendWebhookNotification()) { if ($this->shouldSendWebhookNotification()) {
if ($this->newMicrosoftTeamsWebhookEnabled()) { if ($this->newMicrosoftTeamsWebhookEnabled()) {
$message = $this->getCheckoutNotification($event)->toMicrosoftTeams(); $message = $this->getCheckoutNotification($event)->toMicrosoftTeams();
$notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint); $notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint);
$notification->success()->sendMessage($message[0], $message[1]); // Send the message to Microsoft Teams $notification->success()->sendMessage($message[0], $message[1]); // Send the message to Microsoft Teams
} else { } else {
Notification::route($this->webhookSelected(), Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckoutNotification($event, $acceptance)); Notification::route($this->webhookSelected(), Setting::getSettings()->webhook_endpoint)
} ->notify($this->getCheckoutNotification($event, $acceptance));
} }
}
} catch (ClientException $e) { } catch (ClientException $e) {
Log::debug("Exception caught during checkout notification: " . $e->getMessage()); Log::error("ClientException caught during checkin notification: " . $e->getMessage());
return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected) .trans('admin/settings/message.webhook.webhook_fail') );
} catch (Exception $e) { } catch (Exception $e) {
Log::debug("Exception caught during checkout notification: " . $e->getMessage()); Log::error(ucfirst(Setting::getSettings()->webhook_selected) . ' webhook notification failed:', [
'error' => $e->getMessage(),
'webhook_endpoint' => Setting::getSettings()->webhook_endpoint,
'event' => $event,
]);
return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected) . trans('admin/settings/message.webhook.webhook_fail'));
} }
} }
/** /**
* Notify the user and post to webhook about the checked in checkoutable * Notify the user and post to webhook about the checked in checkoutable
*/ */
@ -178,18 +187,24 @@ class CheckoutableListener
try { try {
if ($this->shouldSendWebhookNotification()) { if ($this->shouldSendWebhookNotification()) {
if ($this->newMicrosoftTeamsWebhookEnabled()) { if ($this->newMicrosoftTeamsWebhookEnabled()) {
$message = $this->getCheckinNotification($event)->toMicrosoftTeams(); $message = $this->getCheckinNotification($event)->toMicrosoftTeams();
$notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint); $notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint);
$notification->success()->sendMessage($message[0], $message[1]); // Send the message to Microsoft Teams $notification->success()->sendMessage($message[0], $message[1]); // Send the message to Microsoft Teams
} else { } else {
Notification::route($this->webhookSelected(), Setting::getSettings()->webhook_endpoint) Notification::route($this->webhookSelected(), Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckinNotification($event)); ->notify($this->getCheckinNotification($event));
}
} }
}
} catch (ClientException $e) { } catch (ClientException $e) {
Log::warning("Exception caught during checkin notification: " . $e->getMessage()); Log::error("ClientException caught during checkin notification: " . $e->getMessage());
return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected) .trans('admin/settings/message.webhook.webhook_fail'));
} catch (Exception $e) { } catch (Exception $e) {
Log::warning("Exception caught during checkin notification: " . $e->getMessage()); Log::error(ucfirst(Setting::getSettings()->webhook_selected) . ' webhook notification failed:', [
'error' => $e->getMessage(),
'webhook_endpoint' => Setting::getSettings()->webhook_endpoint,
'event' => $event,
]);
return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected) .trans('admin/settings/message.webhook.webhook_fail'));
} }
} }

View file

@ -45,5 +45,6 @@ return [
'error' => 'Something went wrong. :app responded with: :error_message', 'error' => 'Something went wrong. :app responded with: :error_message',
'error_redirect' => 'ERROR: 301/302 :endpoint returns a redirect. For security reasons, we dont follow redirects. Please use the actual endpoint.', 'error_redirect' => 'ERROR: 301/302 :endpoint returns a redirect. For security reasons, we dont follow redirects. Please use the actual endpoint.',
'error_misc' => 'Something went wrong. :( ', 'error_misc' => 'Something went wrong. :( ',
'webhook_fail' => ' webhook notification failed: Check to make sure the URL is still valid.',
] ]
]; ];