Skip to content

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 committed May 8, 2022
1 parent 7de5d48 commit 625929e
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions agent/src/agents/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function getTwitchUserId(username: string): Promise<string> {
}
);
const json = await res.json();
if (json.data.length === 0) {
if ((json.data || []).length === 0) {
throw new Error("user not found");
}
return json.data[0]["id"];
Expand Down Expand Up @@ -192,7 +192,9 @@ async function getChatAgent(
isAction,
...tmiJsTagsShim(message, isAction),
}
);
).catch((err) => {
log.error(err);
});
});

twitch.chat.on(TwitchJs.Chat.Events.CLEAR_MESSAGE, (message) => {
Expand All @@ -204,18 +206,24 @@ async function getChatAgent(
message.tags.targetMsgId,
message.timestamp,
message.tags
);
).catch((err) => {
log.error(err);
});
});

// twitch.chat.on(TwitchJs.Chat.Events.CLEAR_CHAT, async (message) => {
// await firebase
// .getMessage(`twitch:clear-${message.timestamp.toISOString()}`)
// .set({
// channel: message.channel,
// channelId: `twitch:${await getTwitchUserId(message.channel)}`,
// type: "clear",
// });
// });
twitch.chat.on(TwitchJs.Chat.Events.CLEAR_CHAT, (message) => {
(async () => {
await firebase
.getMessage(`twitch:clear-${message.timestamp.toISOString()}`)
.set({
channel: message.channel,
channelId: `twitch:${await getTwitchUserId(message.channel)}`,
type: "clear",
});
})().catch((err) => {
log.error(err);
});
});

twitch.chat.on(TwitchJs.Chat.Events.ALL, (message) => {
if (message.event.startsWith("HOSTED/")) {
Expand All @@ -225,7 +233,9 @@ async function getChatAgent(
(message as any).tags.displayName,
message.timestamp,
(message as any).numberOfViewers || 0
);
).catch((err) => {
log.error(err);
});
}
});

Expand Down Expand Up @@ -348,9 +358,13 @@ export async function runTwitchAgent(
}
channels.add(channel);
promises.push(
join(firebase, agentId, channel).then(() => {
channels.delete(channel);
})
join(firebase, agentId, channel)
.catch((e) => {
log.error({ channel, agentId, provider }, e);
})
.finally(() => {
channels.delete(channel);
})
);
});

Expand Down

0 comments on commit 625929e

Please sign in to comment.