From 3bbf42eedb61bf308bc2db64a8b3c4cba742c8fb Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Tue, 15 Oct 2024 19:30:00 +1100 Subject: [PATCH] feat: adding clearer error when a notification is rejected due to permissions --- src/notifications/NotificationsManager.ts | 19 +++++++++++++++++-- src/notifications/errors.ts | 6 ++++++ .../NotificationsManager.test.ts | 16 +++++++++------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/notifications/NotificationsManager.ts b/src/notifications/NotificationsManager.ts index c3ea4684a..e7620b7c1 100644 --- a/src/notifications/NotificationsManager.ts +++ b/src/notifications/NotificationsManager.ts @@ -26,6 +26,7 @@ import * as notificationsUtils from './utils'; import * as notificationsErrors from './errors'; import * as notificationsEvents from './events'; import config from '../config'; +import { ErrorPolykeyRemote } from '../network/errors'; import * as nodesUtils from '../nodes/utils'; import { never } from '../utils/utils'; @@ -385,8 +386,22 @@ class NotificationsManager { tran, ); const sendP = (async () => { - while (pendingTask != null) { - pendingTask = await pendingTask.promise(); + try { + while (pendingTask != null) { + pendingTask = await pendingTask.promise(); + } + } catch (e) { + if ( + e instanceof ErrorPolykeyRemote && + e.cause instanceof + notificationsErrors.ErrorNotificationsPermissionsNotFound + ) { + throw new notificationsErrors.ErrorNotificationsNotificationRejected( + undefined, + { cause: e }, + ); + } + throw e; } })(); sendP.catch(() => {}); diff --git a/src/notifications/errors.ts b/src/notifications/errors.ts index be9b40243..d4e66b213 100644 --- a/src/notifications/errors.ts +++ b/src/notifications/errors.ts @@ -23,6 +23,11 @@ class ErrorNotificationsPermissionsNotFound extends ErrorNotifications { exitCode = sysexits.NOUSER; } +class ErrorNotificationsNotificationRejected extends ErrorNotifications { + static description = 'Notification was rejected due to lack of permissions'; + exitCode = sysexits.NOPERM; +} + class ErrorNotificationsDb extends ErrorNotifications { static description = 'Database consistency error'; exitCode = sysexits.IOERR; @@ -79,6 +84,7 @@ export { ErrorNotificationsNotRunning, ErrorNotificationsDestroyed, ErrorNotificationsPermissionsNotFound, + ErrorNotificationsNotificationRejected, ErrorNotificationsDb, ErrorNotificationsParse, ErrorNotificationsInvalidType, diff --git a/tests/notifications/NotificationsManager.test.ts b/tests/notifications/NotificationsManager.test.ts index bf66d5908..363adee2d 100644 --- a/tests/notifications/NotificationsManager.test.ts +++ b/tests/notifications/NotificationsManager.test.ts @@ -345,8 +345,7 @@ describe('NotificationsManager', () => { pull: null, } as VaultActions, }; - - await testUtils.expectRemoteError( + await expect( notificationsManager .sendNotification({ nodeId: receiver.keyRing.getNodeId(), @@ -354,9 +353,10 @@ describe('NotificationsManager', () => { retries: 0, }) .then((value) => value.sendP), - notificationsErrors.ErrorNotificationsPermissionsNotFound, + ).rejects.toThrow( + notificationsErrors.ErrorNotificationsNotificationRejected, ); - await testUtils.expectRemoteError( + await expect( notificationsManager .sendNotification({ nodeId: receiver.keyRing.getNodeId(), @@ -364,9 +364,10 @@ describe('NotificationsManager', () => { retries: 0, }) .then((value) => value.sendP), - notificationsErrors.ErrorNotificationsPermissionsNotFound, + ).rejects.toThrow( + notificationsErrors.ErrorNotificationsNotificationRejected, ); - await testUtils.expectRemoteError( + await expect( notificationsManager .sendNotification({ nodeId: receiver.keyRing.getNodeId(), @@ -374,7 +375,8 @@ describe('NotificationsManager', () => { retries: 0, }) .then((value) => value.sendP), - notificationsErrors.ErrorNotificationsPermissionsNotFound, + ).rejects.toThrow( + notificationsErrors.ErrorNotificationsNotificationRejected, ); await expect( notificationsManager