@@ -11,6 +11,7 @@ import { phoneToJid } from "../Utils";
11
11
import { createDelay } from "../Utils/create-delay" ;
12
12
import { isExist } from "../Utils/is-exist" ;
13
13
import mime from "mime" ;
14
+ import { WhatsappError } from "../Error" ;
14
15
15
16
export const sendTextMessage = async ( {
16
17
sessionId,
@@ -20,7 +21,7 @@ export const sendTextMessage = async ({
20
21
...props
21
22
} : SendMessageTypes ) : Promise < proto . WebMessageInfo | undefined > => {
22
23
const session = getSession ( sessionId ) ;
23
- if ( ! session ) throw new Error ( Messages . sessionNotFound ( sessionId ) ) ;
24
+ if ( ! session ) throw new WhatsappError ( Messages . sessionNotFound ( sessionId ) ) ;
24
25
const oldPhone = to ;
25
26
to = phoneToJid ( { to, isGroup } ) ;
26
27
const isRegistered = await isExist ( {
@@ -29,7 +30,7 @@ export const sendTextMessage = async ({
29
30
isGroup,
30
31
} ) ;
31
32
if ( ! isRegistered ) {
32
- throw new Error ( `${ oldPhone } is not registered on Whatsapp` ) ;
33
+ throw new WhatsappError ( `${ oldPhone } is not registered on Whatsapp` ) ;
33
34
}
34
35
return await session . sendMessage (
35
36
to ,
@@ -50,7 +51,7 @@ export const sendImage = async ({
50
51
...props
51
52
} : SendMediaTypes ) : Promise < proto . WebMessageInfo | undefined > => {
52
53
const session = getSession ( sessionId ) ;
53
- if ( ! session ) throw new Error ( Messages . sessionNotFound ( sessionId ) ) ;
54
+ if ( ! session ) throw new WhatsappError ( Messages . sessionNotFound ( sessionId ) ) ;
54
55
const oldPhone = to ;
55
56
to = phoneToJid ( { to, isGroup } ) ;
56
57
const isRegistered = await isExist ( {
@@ -59,9 +60,10 @@ export const sendImage = async ({
59
60
isGroup,
60
61
} ) ;
61
62
if ( ! isRegistered ) {
62
- throw new Error ( `${ oldPhone } is not registered on Whatsapp` ) ;
63
+ throw new WhatsappError ( `${ oldPhone } is not registered on Whatsapp` ) ;
63
64
}
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" ) ;
65
67
return await session . sendMessage (
66
68
to ,
67
69
{
@@ -87,7 +89,7 @@ export const sendVideo = async ({
87
89
...props
88
90
} : SendMediaTypes ) : Promise < proto . WebMessageInfo | undefined > => {
89
91
const session = getSession ( sessionId ) ;
90
- if ( ! session ) throw new Error ( Messages . sessionNotFound ( sessionId ) ) ;
92
+ if ( ! session ) throw new WhatsappError ( Messages . sessionNotFound ( sessionId ) ) ;
91
93
const oldPhone = to ;
92
94
to = phoneToJid ( { to, isGroup } ) ;
93
95
const isRegistered = await isExist ( {
@@ -96,9 +98,10 @@ export const sendVideo = async ({
96
98
isGroup,
97
99
} ) ;
98
100
if ( ! isRegistered ) {
99
- throw new Error ( `${ oldPhone } is not registered on Whatsapp` ) ;
101
+ throw new WhatsappError ( `${ oldPhone } is not registered on Whatsapp` ) ;
100
102
}
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" ) ;
102
105
return await session . sendMessage (
103
106
to ,
104
107
{
@@ -128,7 +131,7 @@ export const sendDocument = async ({
128
131
filename : string ;
129
132
} ) : Promise < proto . WebMessageInfo | undefined > => {
130
133
const session = getSession ( sessionId ) ;
131
- if ( ! session ) throw new Error ( Messages . sessionNotFound ( sessionId ) ) ;
134
+ if ( ! session ) throw new WhatsappError ( Messages . sessionNotFound ( sessionId ) ) ;
132
135
const oldPhone = to ;
133
136
to = phoneToJid ( { to, isGroup } ) ;
134
137
const isRegistered = await isExist ( {
@@ -137,15 +140,15 @@ export const sendDocument = async ({
137
140
isGroup,
138
141
} ) ;
139
142
if ( ! isRegistered ) {
140
- throw new Error ( `${ oldPhone } is not registered on Whatsapp` ) ;
143
+ throw new WhatsappError ( `${ oldPhone } is not registered on Whatsapp` ) ;
141
144
}
142
145
if ( ! media || ! Buffer . isBuffer ( media ) ) {
143
- throw new Error ( `Media File must be Buffer` ) ;
146
+ throw new WhatsappError ( `Media File must be Buffer` ) ;
144
147
}
145
148
146
149
const mimetype = mime . getType ( filename ) ;
147
150
if ( ! mimetype ) {
148
- throw new Error ( `Filename must include valid extension` ) ;
151
+ throw new WhatsappError ( `Filename must include valid extension` ) ;
149
152
}
150
153
151
154
return await session . sendMessage (
@@ -181,14 +184,14 @@ export const sendTyping = async ({
181
184
const oldPhone = to ;
182
185
to = phoneToJid ( { to, isGroup } ) ;
183
186
const session = getSession ( sessionId ) ;
184
- if ( ! session ) throw new Error ( Messages . sessionNotFound ( sessionId ) ) ;
187
+ if ( ! session ) throw new WhatsappError ( Messages . sessionNotFound ( sessionId ) ) ;
185
188
const isRegistered = await isExist ( {
186
189
sessionId,
187
190
to,
188
191
isGroup,
189
192
} ) ;
190
193
if ( ! isRegistered ) {
191
- throw new Error ( `${ oldPhone } is not registered on Whatsapp` ) ;
194
+ throw new WhatsappError ( `${ oldPhone } is not registered on Whatsapp` ) ;
192
195
}
193
196
await session . sendPresenceUpdate ( "composing" , to ) ;
194
197
await createDelay ( duration ) ;
@@ -207,7 +210,7 @@ export const sendTyping = async ({
207
210
*/
208
211
export const readMessage = async ( { sessionId, key } : SendReadTypes ) => {
209
212
const session = getSession ( sessionId ) ;
210
- if ( ! session ) throw new Error ( Messages . sessionNotFound ( sessionId ) ) ;
213
+ if ( ! session ) throw new WhatsappError ( Messages . sessionNotFound ( sessionId ) ) ;
211
214
212
215
await session . readMessages ( [ key ] ) ;
213
216
} ;
0 commit comments