Skip to content

Commit 444bdd7

Browse files
Merge pull request #1642 from nextcloud/techdebt/noid/improved-activity-exceptions
fix(activity): Improved activity exceptions
2 parents 6cf61d1 + dfb43f7 commit 444bdd7

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

docs/provider.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ The first thing the provider should do, is to check whether the `IEvent` is one
3737

3838
```php
3939
if ($event->getApp() !== 'files' || $event->getType() !== 'favorite') {
40-
throw new \InvalidArgumentException();
40+
throw new \OCP\Activity\Exceptions\UnknownActivityException();
4141
}
4242
```
4343

44-
Whenever a provider throws an `\InvalidArgumentException` the activity app will continue and pass the event to the next provider, so this should always be thrown when the event is unknown.
44+
Whenever a provider throws an `UnknownActivityException` (*Added in Nextcloud 30, before throw `\InvalidArgumentException`*), the activity app will continue and pass the event to the next provider, so this should always be thrown when the event is unknown.
4545

4646
### Short translation
4747

lib/Data.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
use Doctrine\DBAL\Platforms\MySQLPlatform;
3030
use OCA\Activity\Filter\AllFilter;
31+
use OCP\Activity\Exceptions\FilterNotFoundException;
3132
use OCP\Activity\IEvent;
3233
use OCP\Activity\IExtension;
3334
use OCP\Activity\IFilter;
@@ -181,7 +182,7 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user,
181182
$activeFilter = null;
182183
try {
183184
$activeFilter = $this->activityManager->getFilterById($filter);
184-
} catch (\InvalidArgumentException $e) {
185+
} catch (FilterNotFoundException) {
185186
// Unknown filter => ignore and show all activities
186187
}
187188

@@ -223,7 +224,7 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user,
223224
$favoriteFilter = $this->activityManager->getFilterById('files_favorites');
224225
/** @var \OCA\Files\Activity\Filter\Favorites $favoriteFilter */
225226
$favoriteFilter->filterFavorites($query);
226-
} catch (\InvalidArgumentException $e) {
227+
} catch (FilterNotFoundException) {
227228
}
228229
}
229230

@@ -334,7 +335,7 @@ public function validateFilter($filterValue) {
334335
try {
335336
$this->activityManager->getFilterById($filterValue);
336337
return $filterValue;
337-
} catch (\InvalidArgumentException $e) {
338+
} catch (FilterNotFoundException) {
338339
return 'all';
339340
}
340341
}

lib/GroupHelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace OCA\Activity;
2424

25+
use OCP\Activity\Exceptions\UnknownActivityException;
2526
use OCP\Activity\IEvent;
2627
use OCP\Activity\IManager;
2728
use OCP\IL10N;
@@ -75,7 +76,11 @@ public function addEvent(int $id, IEvent $event): void {
7576
} else {
7677
$event = $provider->parse($language, $event);
7778
}
78-
} catch (\InvalidArgumentException $e) {
79+
} catch (UnknownActivityException) {
80+
} catch (\InvalidArgumentException) {
81+
// todo 33.0.0 Log as warning
82+
// todo 39.0.0 Log as error
83+
$this->logger->debug(get_class($provider) . '::parse() threw \InvalidArgumentException which is deprecated. Throw \OCP\Activity\Exceptions\UnknownActivityException when the event is not known to your provider and otherwise handle all \InvalidArgumentException yourself.');
7984
} catch (\Throwable $e) {
8085
$this->logger->error('Error while parsing activity event', ['exception' => $e]);
8186
}

lib/NotificationGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
namespace OCA\Activity;
2525

26+
use OCP\Activity\Exceptions\UnknownActivityException;
2627
use OCP\Activity\IEvent;
2728
use OCP\Activity\IManager as ActivityManager;
2829
use OCP\IL10N;
2930
use OCP\Notification\AlreadyProcessedException;
3031
use OCP\Notification\IManager as NotificationManager;
3132
use OCP\Notification\INotification;
3233
use OCP\Notification\INotifier;
34+
use Psr\Log\LoggerInterface;
3335

3436
class NotificationGenerator implements INotifier {
3537

@@ -38,7 +40,9 @@ public function __construct(
3840
protected ActivityManager $activityManager,
3941
protected NotificationManager $notificationManager,
4042
protected UserSettings $userSettings,
41-
protected IL10N $l10n) {
43+
protected IL10N $l10n,
44+
protected LoggerInterface $logger,
45+
) {
4246
}
4347

4448
public function deferNotifications(): bool {
@@ -90,7 +94,11 @@ private function populateEvent(IEvent $event, string $language) {
9094
foreach ($this->activityManager->getProviders() as $provider) {
9195
try {
9296
$event = $provider->parse($language, $event);
97+
} catch (UnknownActivityException) {
9398
} catch (\InvalidArgumentException $e) {
99+
// todo 33.0.0 Log as warning
100+
// todo 39.0.0 Log as error
101+
$this->logger->debug(get_class($provider) . '::parse() threw \InvalidArgumentException which is deprecated. Throw \OCP\Activity\Exceptions\UnknownActivityException when the event is not known to your provider and otherwise handle all \InvalidArgumentException yourself.');
94102
}
95103
}
96104
$this->activityManager->setFormattingObject('', 0);

lib/UserSettings.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace OCA\Activity;
2424

2525
use OCP\Activity\ActivitySettings;
26+
use OCP\Activity\Exceptions\SettingNotFoundException;
2627
use OCP\Activity\IManager;
2728
use OCP\IConfig;
2829

@@ -143,7 +144,7 @@ protected function getDefaultSetting($method, $type) {
143144
default:
144145
return false;
145146
}
146-
} catch (\InvalidArgumentException $e) {
147+
} catch (SettingNotFoundException) {
147148
return false;
148149
}
149150
}
@@ -170,7 +171,7 @@ protected function canModifySetting($method, $type) {
170171
default:
171172
return false;
172173
}
173-
} catch (\InvalidArgumentException $e) {
174+
} catch (SettingNotFoundException) {
174175
return false;
175176
}
176177
}

tests/UserSettingsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
use OCA\Activity\UserSettings;
2626
use OCP\Activity\ActivitySettings;
27+
use OCP\Activity\Exceptions\SettingNotFoundException;
2728
use OCP\Activity\IManager;
2829
use OCP\IConfig;
2930
use PHPUnit\Framework\MockObject\MockObject;
@@ -65,7 +66,7 @@ public function testGetDefaultSetting(string $method, string $type, $expected):
6566
$this->activityManager->expects($this->once())
6667
->method('getSettingById')
6768
->with($type)
68-
->willThrowException(new \InvalidArgumentException());
69+
->willThrowException(new SettingNotFoundException($type));
6970
} else {
7071
$s = $this->createMock(ActivitySettings::class);
7172
$s->expects($method === 'email' ? $this->once() : $this->never())

0 commit comments

Comments
 (0)