Skip to content

Commit

Permalink
feat: adding clearer error when a notification is rejected due to per…
Browse files Browse the repository at this point in the history
…missions
  • Loading branch information
tegefaulkes committed Oct 15, 2024
1 parent 67dfb47 commit 3bbf42e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
19 changes: 17 additions & 2 deletions src/notifications/NotificationsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(() => {});
Expand Down
6 changes: 6 additions & 0 deletions src/notifications/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class ErrorNotificationsPermissionsNotFound<T> extends ErrorNotifications<T> {
exitCode = sysexits.NOUSER;
}

class ErrorNotificationsNotificationRejected<T> extends ErrorNotifications<T> {
static description = 'Notification was rejected due to lack of permissions';
exitCode = sysexits.NOPERM;
}

class ErrorNotificationsDb<T> extends ErrorNotifications<T> {
static description = 'Database consistency error';
exitCode = sysexits.IOERR;
Expand Down Expand Up @@ -79,6 +84,7 @@ export {
ErrorNotificationsNotRunning,
ErrorNotificationsDestroyed,
ErrorNotificationsPermissionsNotFound,
ErrorNotificationsNotificationRejected,
ErrorNotificationsDb,
ErrorNotificationsParse,
ErrorNotificationsInvalidType,
Expand Down
16 changes: 9 additions & 7 deletions tests/notifications/NotificationsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,36 +345,38 @@ describe('NotificationsManager', () => {
pull: null,
} as VaultActions,
};

await testUtils.expectRemoteError(
await expect(
notificationsManager
.sendNotification({
nodeId: receiver.keyRing.getNodeId(),
data: generalNotification,
retries: 0,
})
.then((value) => value.sendP),
notificationsErrors.ErrorNotificationsPermissionsNotFound,
).rejects.toThrow(
notificationsErrors.ErrorNotificationsNotificationRejected,
);
await testUtils.expectRemoteError(
await expect(
notificationsManager
.sendNotification({
nodeId: receiver.keyRing.getNodeId(),
data: gestaltNotification,
retries: 0,
})
.then((value) => value.sendP),
notificationsErrors.ErrorNotificationsPermissionsNotFound,
).rejects.toThrow(
notificationsErrors.ErrorNotificationsNotificationRejected,
);
await testUtils.expectRemoteError(
await expect(
notificationsManager
.sendNotification({
nodeId: receiver.keyRing.getNodeId(),
data: vaultNotification,
retries: 0,
})
.then((value) => value.sendP),
notificationsErrors.ErrorNotificationsPermissionsNotFound,
).rejects.toThrow(
notificationsErrors.ErrorNotificationsNotificationRejected,
);
await expect(
notificationsManager
Expand Down

0 comments on commit 3bbf42e

Please sign in to comment.