Skip to content

Commit 5348b70

Browse files
committed
added channel not supported error
1 parent 7ec0392 commit 5348b70

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

packages/backend/src/notifications.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { normalizeEnsName } from '@dm3-org/dm3-lib-profile';
44
import express from 'express';
55
import { auth } from './utils';
66
import { validateNotificationChannel } from './validation/notification/notificationChannelValidation';
7-
import { addNewNotificationChannel } from '@dm3-org/dm3-lib-delivery';
7+
import {
8+
ChannelNotSupportedError,
9+
addNewNotificationChannel,
10+
} from '@dm3-org/dm3-lib-delivery';
811
import { getDeliveryServiceProperties } from './config/getDeliveryServiceProperties';
912
import { IDatabase } from './persistance/getDatabase';
1013

@@ -65,12 +68,12 @@ export default () => {
6568

6669
// Defining a route to handle POST requests for adding an notification channel
6770
router.post('/:ensName', async (req, res, next) => {
71+
// Extracting recipientValue & notificationChannelType from the request body
72+
const { recipientValue, notificationChannelType } = req.body;
73+
6874
try {
6975
const account = normalizeEnsName(req.params.ensName);
7076

71-
// Extracting recipientValue & notificationChannelType from the request body
72-
const { recipientValue, notificationChannelType } = req.body;
73-
7477
// Validate req.body data
7578
const { isValid, errorMessage } = validateNotificationChannel(
7679
notificationChannelType,
@@ -106,9 +109,19 @@ export default () => {
106109
// Sending a success response
107110
res.sendStatus(200);
108111
}
109-
} catch (e) {
110-
// Passing the error to the next middleware
111-
next(e);
112+
} catch (e: any) {
113+
if (
114+
e instanceof ChannelNotSupportedError ||
115+
e.message === 'Invalid config.yml'
116+
) {
117+
// return the error for not supported channels
118+
res.sendStatus(400).json({
119+
error: `Notification channel ${notificationChannelType} is currently not supported yet by the DS`,
120+
});
121+
} else {
122+
// Passing the error to the next middleware
123+
next(e);
124+
}
112125
}
113126
});
114127

packages/lib/delivery/src/Notification.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ChannelNotSupportedError } from './errors/ChannelNotSupportedError';
12
import {
23
NotificationBroker,
34
NotificationChannel,
@@ -42,7 +43,9 @@ export async function addNewNotificationChannel(
4243
);
4344

4445
if (!channelUsed.length) {
45-
throw Error('Notification channel not supported');
46+
throw new ChannelNotSupportedError(
47+
'Notification channel not supported',
48+
);
4649
}
4750

4851
// Adding a user's notification channel to the database
@@ -82,7 +85,7 @@ const getOtpContentForNotificationChannel = (
8285
dm3ContactEmailID: notificationChannel.config.smtpEmail,
8386
};
8487
default:
85-
throw Error(
88+
throw new ChannelNotSupportedError(
8689
`Invalid notification channel ${notificationChannel.type}`,
8790
);
8891
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Error class for channels not supported by delivery services
2+
export class ChannelNotSupportedError extends Error {
3+
constructor(msg: string) {
4+
super(msg);
5+
Object.setPrototypeOf(this, ChannelNotSupportedError.prototype);
6+
}
7+
}

packages/lib/delivery/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export type { DeliveryServiceProperties } from './Delivery';
1818
export * from './notifications';
1919
export { NotificationChannelType } from './notifications';
2020
export { addNewNotificationChannel } from './Notification';
21+
export { ChannelNotSupportedError } from './errors/ChannelNotSupportedError';

packages/lib/delivery/src/notifications/broker/NotificationBroker.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
INotificationChannel,
99
NotificationType,
1010
} from '../types';
11+
import { ChannelNotSupportedError } from '../../errors/ChannelNotSupportedError';
1112

1213
/**
1314
* Sets up the notification broker with supported notification channels.
@@ -35,7 +36,7 @@ export const _setupNotficationBroker = (
3536
supportedChannels.find((c) => c.type === channel.type);
3637
//User specified a channel that is not supported
3738
if (!deliveryServiceNotificationChannel) {
38-
throw new Error(
39+
throw new ChannelNotSupportedError(
3940
`Channel type ${channel.type} is not supported`,
4041
);
4142
}
@@ -73,7 +74,7 @@ export const _setupNotficationBroker = (
7374
);
7475

7576
if (!deliveryServiceNotificationChannel) {
76-
throw new Error(
77+
throw new ChannelNotSupportedError(
7778
`Channel type ${filteredChannel[0].type} is not supported`,
7879
);
7980
} else {
@@ -107,7 +108,7 @@ export const NotificationBroker = (
107108
send: Email(channel.config).send,
108109
};
109110
default:
110-
throw new Error(
111+
throw new ChannelNotSupportedError(
111112
`Channel type ${channel.type} is not supported`,
112113
);
113114
}

0 commit comments

Comments
 (0)