Skip to content

Commit 7eedeed

Browse files
committed
Add only retry on 429 error, add DOMAIN to .env.example
1 parent 3269b9e commit 7eedeed

File tree

6 files changed

+61
-25
lines changed

6 files changed

+61
-25
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ NUMBER_POOL=["+1234567890","+2345678901"]
44
CALL_ANNOUCEMENT_VOICE=
55
CALL_ANNOUCEMENT_LANGUAGE=
66
OUT_OF_SESSION_MESSAGE_FOR_CALL=
7-
CONNECTING_CALL_ANNOUCEMENT=
7+
CONNECTING_CALL_ANNOUCEMENT=
8+
DOMAIN=

src/utils/addParticipant.util.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const addParticipant = async (
66
conversationSid: string,
77
participant: ParticipantListInstanceCreateOptions
88
) : Promise<ParticipantInstance> => {
9-
return retry(async () => {
9+
return retry(async (quit) => {
1010
try {
1111
const createdParticipant = await client.conversations
1212
.conversations(conversationSid)
@@ -15,8 +15,13 @@ export const addParticipant = async (
1515

1616
return createdParticipant
1717
} catch (err) {
18-
console.log('Create participant err', err);
19-
throw new Error(err)
18+
if (err.status !== 429) {
19+
quit(new Error(err));
20+
return;
21+
}
22+
23+
console.log('Re-trying on 429 error');
24+
throw new Error(err);
2025
}
2126
})
2227
}

src/utils/createConversation.util.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ import client from "../twilioClient";
55
import retry from 'async-retry';
66

77
export const createConversation = async (options: SessionPostBody) : Promise<ConversationInstance> => {
8-
return retry(async () => {
8+
return retry(async (quit) => {
99
try {
1010
const conversation = await client.conversations.conversations.create(options);
1111
return conversation;
1212
} catch (err) {
13-
throw `createConversation: ${err}`;
13+
if (err.status !== 429) {
14+
quit(new Error(err));
15+
return;
16+
}
17+
18+
console.log('Re-trying on 429 error');
19+
throw new Error(err);
1420
}
1521
})
1622
}

src/utils/deleteConversation.util.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import client from "../twilioClient"
33
import retry from 'async-retry'
44

55
export const deleteConversation = async (conversationSid: string) : Promise<boolean> => {
6-
return retry(async() => {
6+
return retry(async(quit) => {
77
try {
88
await client.conversations.conversations(conversationSid).remove()
99
return true
1010
} catch (err) {
11-
throw err
11+
if (err.status !== 429) {
12+
quit(new Error(err));
13+
return;
14+
}
15+
16+
console.log('Re-trying on 429 error');
17+
throw new Error(err);
1218
}
1319
})
1420
}
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation';
2+
import retry from 'async-retry'
23
import client from '../twilioClient'
34

45
export const getConversationByAddressPair = async (address: string, proxyAddress: string) : Promise<ParticipantConversationInstance> => {
5-
if (address === undefined) {
6-
throw "getOpenConversationsForAddressPair: address is missing";
7-
}
6+
return retry(async(quit) => {
7+
try {
8+
if (address === undefined) {
9+
throw "getOpenConversationsForAddressPair: address is missing";
10+
}
11+
12+
const participantConversations = await client.conversations.v1
13+
.participantConversations
14+
.list({ address: address });
15+
16+
const conversation = participantConversations.find(p => {
17+
if (p.conversationState !== 'closed' && p.participantMessagingBinding.proxy_address === proxyAddress) {
18+
console.log(`Found a non-closed conversation ${p.conversationSid} with proxy address ${p.participantMessagingBinding.proxy_address} for address ${address}`);
19+
return p;
20+
}
21+
});
22+
23+
return conversation;
24+
} catch(err) {
25+
if (err.status !== 429) {
26+
quit(new Error(err));
27+
return;
28+
}
829

9-
const participantConversations = await client.conversations.v1
10-
.participantConversations
11-
.list({ address: address });
12-
13-
const conversation = participantConversations.find(p => {
14-
if (p.conversationState !== 'closed' && p.participantMessagingBinding.proxy_address === proxyAddress) {
15-
console.log(`Found a non-closed conversation ${p.conversationSid} with proxy address ${p.participantMessagingBinding.proxy_address} for address ${address}`);
16-
return p;
30+
console.log('Re-trying on 429 error');
31+
throw new Error(err);
1732
}
18-
});
19-
20-
return conversation;
33+
})
2134
}

src/utils/listParticipantConversations.util.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v
33
import client from '../twilioClient'
44

55
export const listParticipantConversations = async (phoneNumber: string) : Promise<ParticipantConversationInstance[]> => {
6-
return retry(async () => {
6+
return retry(async (quit) => {
77
try {
88
const activeConversations = await client.conversations.participantConversations.list({address: phoneNumber})
99
return activeConversations
1010
} catch (err) {
11-
console.log(phoneNumber, err)
12-
throw new Error(err)
11+
if (err.status !== 429) {
12+
quit(new Error(err));
13+
return;
14+
}
15+
16+
console.log('Re-trying on 429 error');
17+
throw new Error(err);
1318
}
1419
})
1520

0 commit comments

Comments
 (0)