Skip to content

Commit 18ee3f9

Browse files
authored
Merge pull request #10 from mimamch/dev-error-class
v3.2.0 + add error class WhatsappError
2 parents 61a27a5 + 29ef21b commit 18ee3f9

File tree

8 files changed

+36
-20
lines changed

8 files changed

+36
-20
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wa-multi-session",
3-
"version": "3.1.2",
3+
"version": "3.2.0",
44
"description": "Multi Session Whatsapp Library",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ whatsapp.setCredentialsDir("my_custom_dir");
211211

212212
## Change Log
213213

214-
v3.1.2 July 2023 (LATEST)
214+
v3.2.0 July 2023 (LATEST)
215+
216+
- Add error class named: WhatsappError
217+
218+
v3.1.2 July 2023
215219

216220
- Add send document message
217221

src/Error/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class WhatsappError extends Error {
2+
constructor(message: string) {
3+
super(message);
4+
}
5+
}

src/Messaging/index.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { phoneToJid } from "../Utils";
1111
import { createDelay } from "../Utils/create-delay";
1212
import { isExist } from "../Utils/is-exist";
1313
import mime from "mime";
14+
import { WhatsappError } from "../Error";
1415

1516
export const sendTextMessage = async ({
1617
sessionId,
@@ -20,7 +21,7 @@ export const sendTextMessage = async ({
2021
...props
2122
}: SendMessageTypes): Promise<proto.WebMessageInfo | undefined> => {
2223
const session = getSession(sessionId);
23-
if (!session) throw new Error(Messages.sessionNotFound(sessionId));
24+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
2425
const oldPhone = to;
2526
to = phoneToJid({ to, isGroup });
2627
const isRegistered = await isExist({
@@ -29,7 +30,7 @@ export const sendTextMessage = async ({
2930
isGroup,
3031
});
3132
if (!isRegistered) {
32-
throw new Error(`${oldPhone} is not registered on Whatsapp`);
33+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
3334
}
3435
return await session.sendMessage(
3536
to,
@@ -50,7 +51,7 @@ export const sendImage = async ({
5051
...props
5152
}: SendMediaTypes): Promise<proto.WebMessageInfo | undefined> => {
5253
const session = getSession(sessionId);
53-
if (!session) throw new Error(Messages.sessionNotFound(sessionId));
54+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
5455
const oldPhone = to;
5556
to = phoneToJid({ to, isGroup });
5657
const isRegistered = await isExist({
@@ -59,9 +60,10 @@ export const sendImage = async ({
5960
isGroup,
6061
});
6162
if (!isRegistered) {
62-
throw new Error(`${oldPhone} is not registered on Whatsapp`);
63+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
6364
}
64-
if (!media) throw new Error("parameter media must be Buffer or String URL");
65+
if (!media)
66+
throw new WhatsappError("parameter media must be Buffer or String URL");
6567
return await session.sendMessage(
6668
to,
6769
{
@@ -87,7 +89,7 @@ export const sendVideo = async ({
8789
...props
8890
}: SendMediaTypes): Promise<proto.WebMessageInfo | undefined> => {
8991
const session = getSession(sessionId);
90-
if (!session) throw new Error(Messages.sessionNotFound(sessionId));
92+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
9193
const oldPhone = to;
9294
to = phoneToJid({ to, isGroup });
9395
const isRegistered = await isExist({
@@ -96,9 +98,10 @@ export const sendVideo = async ({
9698
isGroup,
9799
});
98100
if (!isRegistered) {
99-
throw new Error(`${oldPhone} is not registered on Whatsapp`);
101+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
100102
}
101-
if (!media) throw new Error("parameter media must be Buffer or String URL");
103+
if (!media)
104+
throw new WhatsappError("parameter media must be Buffer or String URL");
102105
return await session.sendMessage(
103106
to,
104107
{
@@ -128,7 +131,7 @@ export const sendDocument = async ({
128131
filename: string;
129132
}): Promise<proto.WebMessageInfo | undefined> => {
130133
const session = getSession(sessionId);
131-
if (!session) throw new Error(Messages.sessionNotFound(sessionId));
134+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
132135
const oldPhone = to;
133136
to = phoneToJid({ to, isGroup });
134137
const isRegistered = await isExist({
@@ -137,15 +140,15 @@ export const sendDocument = async ({
137140
isGroup,
138141
});
139142
if (!isRegistered) {
140-
throw new Error(`${oldPhone} is not registered on Whatsapp`);
143+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
141144
}
142145
if (!media || !Buffer.isBuffer(media)) {
143-
throw new Error(`Media File must be Buffer`);
146+
throw new WhatsappError(`Media File must be Buffer`);
144147
}
145148

146149
const mimetype = mime.getType(filename);
147150
if (!mimetype) {
148-
throw new Error(`Filename must include valid extension`);
151+
throw new WhatsappError(`Filename must include valid extension`);
149152
}
150153

151154
return await session.sendMessage(
@@ -181,14 +184,14 @@ export const sendTyping = async ({
181184
const oldPhone = to;
182185
to = phoneToJid({ to, isGroup });
183186
const session = getSession(sessionId);
184-
if (!session) throw new Error(Messages.sessionNotFound(sessionId));
187+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
185188
const isRegistered = await isExist({
186189
sessionId,
187190
to,
188191
isGroup,
189192
});
190193
if (!isRegistered) {
191-
throw new Error(`${oldPhone} is not registered on Whatsapp`);
194+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
192195
}
193196
await session.sendPresenceUpdate("composing", to);
194197
await createDelay(duration);
@@ -207,7 +210,7 @@ export const sendTyping = async ({
207210
*/
208211
export const readMessage = async ({ sessionId, key }: SendReadTypes) => {
209212
const session = getSession(sessionId);
210-
if (!session) throw new Error(Messages.sessionNotFound(sessionId));
213+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
211214

212215
await session.readMessages([key]);
213216
};

src/Socket/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
saveImageHandler,
1717
saveVideoHandler,
1818
} from "../Utils/save-media";
19+
import { WhatsappError } from "../Error";
1920

2021
const sessions: Map<string, WASocket> = new Map();
2122

@@ -28,7 +29,7 @@ export const startSession = async (
2829
options: StartSessionParams = { printQR: true }
2930
): Promise<WASocket> => {
3031
if (isSessionExistAndRunning(sessionId))
31-
throw new Error(Messages.sessionAlreadyExist(sessionId));
32+
throw new WhatsappError(Messages.sessionAlreadyExist(sessionId));
3233
const logger = pino({ level: "silent" });
3334

3435
const { version } = await fetchLatestBaileysVersion();

src/Utils/is-exist.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WhatsappError } from "../Error";
12
import { getSession } from "../Socket";
23
import { SendMessageTypes } from "../Types";
34
import { phoneToJid } from "./phone-to-jid";
@@ -9,7 +10,7 @@ export const isExist = async ({
910
}: SendMessageTypes): Promise<boolean> => {
1011
try {
1112
const session = getSession(sessionId);
12-
if (!session) throw new Error("Session ID Not Found!");
13+
if (!session) throw new WhatsappError("Session ID Not Found!");
1314
const receiver = phoneToJid({
1415
to: to,
1516
isGroup: isGroup,

src/Utils/phone-to-jid.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WhatsappError } from "../Error";
12
import { SendMessageTypes } from "../Types";
23

34
export const phoneToJid = ({
@@ -7,7 +8,7 @@ export const phoneToJid = ({
78
to: string | number;
89
isGroup?: boolean;
910
}): string => {
10-
if (!to) throw new Error('parameter "to" is required');
11+
if (!to) throw new WhatsappError('parameter "to" is required');
1112
let number = to.toString();
1213
if (isGroup) {
1314
number = number.replace(/\s|[+]|[-]/gim, "");

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./Socket";
22
export * from "./Messaging";
33
export * from "./Utils";
44
export * from "./Types";
5+
export * from "./Error";

0 commit comments

Comments
 (0)