From 0d0c2cdaa08f6bc8f547b437a5b046914680b3f5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 10 Apr 2024 17:12:31 +0200 Subject: [PATCH] fix(notifications): Add a dedicated exception when a notification was not parsed completely Signed-off-by: Joas Schilling --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Notification/Manager.php | 6 ++- lib/public/Notification/INotifier.php | 3 ++ .../IncompleteParsedNotificationException.php | 46 +++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 lib/public/Notification/IncompleteParsedNotificationException.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index cc9aaa6c1f6..a726e5a54d5 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -568,6 +568,7 @@ return array( 'OCP\\Notification\\INotification' => $baseDir . '/lib/public/Notification/INotification.php', 'OCP\\Notification\\INotifier' => $baseDir . '/lib/public/Notification/INotifier.php', 'OCP\\Notification\\IncompleteNotificationException' => $baseDir . '/lib/public/Notification/IncompleteNotificationException.php', + 'OCP\\Notification\\IncompleteParsedNotificationException' => $baseDir . '/lib/public/Notification/IncompleteParsedNotificationException.php', 'OCP\\Notification\\InvalidValueException' => $baseDir . '/lib/public/Notification/InvalidValueException.php', 'OCP\\Notification\\UnknownNotificationException' => $baseDir . '/lib/public/Notification/UnknownNotificationException.php', 'OCP\\OCM\\Events\\ResourceTypeRegisterEvent' => $baseDir . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d37f75b8496..1f29b6cb3b6 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -601,6 +601,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Notification\\INotification' => __DIR__ . '/../../..' . '/lib/public/Notification/INotification.php', 'OCP\\Notification\\INotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/INotifier.php', 'OCP\\Notification\\IncompleteNotificationException' => __DIR__ . '/../../..' . '/lib/public/Notification/IncompleteNotificationException.php', + 'OCP\\Notification\\IncompleteParsedNotificationException' => __DIR__ . '/../../..' . '/lib/public/Notification/IncompleteParsedNotificationException.php', 'OCP\\Notification\\InvalidValueException' => __DIR__ . '/../../..' . '/lib/public/Notification/InvalidValueException.php', 'OCP\\Notification\\UnknownNotificationException' => __DIR__ . '/../../..' . '/lib/public/Notification/UnknownNotificationException.php', 'OCP\\OCM\\Events\\ResourceTypeRegisterEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.php', diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php index 63374cea21f..df9d338beb0 100644 --- a/lib/private/Notification/Manager.php +++ b/lib/private/Notification/Manager.php @@ -36,6 +36,7 @@ use OCP\Notification\IDeferrableApp; use OCP\Notification\IDismissableNotifier; use OCP\Notification\IManager; use OCP\Notification\IncompleteNotificationException; +use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; use OCP\Notification\INotifier; use OCP\Notification\UnknownNotificationException; @@ -365,13 +366,14 @@ class Manager implements IManager { } if (!$notification->isValidParsed()) { - throw new \InvalidArgumentException('The given notification has not been handled'); + $this->logger->info('Notification was claimed to be parsed, but was not fully parsed by ' . get_class($notifier) . ' [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); + throw new IncompleteParsedNotificationException(); } } if (!$notification->isValidParsed()) { $this->logger->info('Notification was not parsed by any notifier [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); - throw new \InvalidArgumentException('The given notification has not been handled'); + throw new IncompleteParsedNotificationException(); } return $notification; diff --git a/lib/public/Notification/INotifier.php b/lib/public/Notification/INotifier.php index 8be9ee1bdb0..2014f73d5aa 100644 --- a/lib/public/Notification/INotifier.php +++ b/lib/public/Notification/INotifier.php @@ -52,10 +52,13 @@ interface INotifier { * @return INotification * @throws UnknownNotificationException When the notification was not prepared by a notifier * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted + * @throws IncompleteParsedNotificationException Only to be thrown by the {@see IManager} * @since 9.0.0 * @since 30.0.0 Notifiers should throw {@see UnknownNotificationException} instead of \InvalidArgumentException * when they did not handle the notification. Throwing \InvalidArgumentException directly is deprecated and will * be logged as an error in Nextcloud 39. + * @since 30.0.0 Throws {@see IncompleteParsedNotificationException} when not all required fields + * are set at the end of the manager or after a INotifier that claimed to have parsed the notification. */ public function prepare(INotification $notification, string $languageCode): INotification; } diff --git a/lib/public/Notification/IncompleteParsedNotificationException.php b/lib/public/Notification/IncompleteParsedNotificationException.php new file mode 100644 index 00000000000..0a8ca8a61e6 --- /dev/null +++ b/lib/public/Notification/IncompleteParsedNotificationException.php @@ -0,0 +1,46 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Notification; + +/** + * Thrown when {@see \OCP\Notification\IManager::prepare()} is called with a notification + * that does not have all required fields set at the end of the manager or after a INotifier + * that claimed to have parsed the notification. + * + * Required fields are: + * + * - app + * - user + * - dateTime + * - objectType + * - objectId + * - parsedSubject + * + * @since 30.0.0 + */ +class IncompleteParsedNotificationException extends \InvalidArgumentException { +}