@@ -21,6 +21,11 @@ function YemotRouter (options = {}) {
21
21
timeout : options . timeout || 0 ,
22
22
uncaughtErrorsHandler : options . uncaughtErrorsHandler || null
23
23
} ;
24
+
25
+ if ( ops . timeout && Number . isNaN ( ops . timeout ) ) {
26
+ throw new Error ( 'YemotRouter: if you set timeout option, it must be a number' ) ;
27
+ }
28
+
24
29
const activeCalls = { } ;
25
30
const eventsEmitter = new EventEmitter ( ) ;
26
31
@@ -29,43 +34,32 @@ function YemotRouter (options = {}) {
29
34
console . log ( colors [ color ] ( `[${ callId } ]: ${ msg } ` ) ) ;
30
35
}
31
36
32
- function onFnDone ( callId ) {
33
- delete activeCalls [ callId ] ;
34
- logger ( callId , '🆗 the function is done' ) ;
35
- }
36
-
37
- function onExit ( callId ) {
38
- delete activeCalls [ callId ] ;
39
- logger ( callId , '👋 call is exit' ) ;
40
- }
41
-
42
- function onHangup ( callId ) {
43
- delete activeCalls [ callId ] ;
44
- logger ( callId , '👋 call is hangup' ) ;
45
- }
46
-
47
- function onTimeout ( callId ) {
37
+ function deleteCall ( callId ) {
38
+ if ( activeCalls [ callId ] ?. _timeoutId ) {
39
+ clearTimeout ( activeCalls [ callId ] . _timeoutId ) ;
40
+ }
48
41
delete activeCalls [ callId ] ;
49
- logger ( callId , 'timeout for receiving a response from the caller. the call has been deleted from active calls' , 'red' ) ;
50
42
}
51
43
52
44
async function makeNewCall ( fn , callId , call ) {
53
45
try {
54
46
await fn ( call ) ;
55
- onFnDone ( callId ) ;
47
+ deleteCall ( callId ) ;
48
+ logger ( callId , '🆗 the function is done' ) ;
56
49
} catch ( error ) {
50
+ deleteCall ( callId ) ;
51
+
57
52
// טיפול בשגיאות מלאכותיות שנועדו לעצור את ריצת הפונקציה - בניתוק לדוגמה
58
53
if ( error instanceof HangupError ) {
59
- onHangup ( callId ) ;
54
+ logger ( callId , '👋 call is hangup' ) ;
60
55
} else if ( error instanceof TimeoutError ) {
61
- onTimeout ( callId ) ;
56
+ logger ( callId , 'the timeout for the caller\'s answer has expired. the call function has killed and been removed from the active calls' ) ;
62
57
} else if ( error instanceof ExitError ) {
63
- onExit ( callId ) ;
58
+ logger ( callId , '👋 call is exit' ) ;
64
59
} else {
65
60
if ( ops . uncaughtErrorsHandler ) {
66
61
logger ( callId , '💥 Uncaught error. applying uncaughtErrorsHandler' , 'red' ) ;
67
62
ops . uncaughtErrorsHandler ( error , call ) ;
68
- delete activeCalls [ callId ] ;
69
63
} else {
70
64
logger ( callId , '💥 Uncaught error. no uncaughtErrorsHandler, throwing error' , 'red' ) ;
71
65
throw error ;
@@ -90,7 +84,7 @@ function YemotRouter (options = {}) {
90
84
const values = req . method === 'POST' ? req . body : req . query ;
91
85
const requiredValues = [ 'ApiPhone' , 'ApiDID' , 'ApiExtension' , 'ApiCallId' ] ;
92
86
if ( requiredValues . some ( ( key ) => ! Object . prototype . hasOwnProperty . call ( values , key ) ) ) {
93
- return res . json ( { message : 'request is not valid yemot request' } ) ;
87
+ return res . json ( { message : 'the request is not valid yemot request' } ) ;
94
88
}
95
89
96
90
if ( req . method === 'POST' ) {
0 commit comments