From dd423bdb344bea065f421c97329323ca97cf3675 Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Mon, 28 Feb 2022 09:34:18 -0700 Subject: [PATCH] Fix empty subscriptions page crash Fix issue where when Twitch returns an empty page of subscribers, the app crashes due to trying to get response.data[0].user_id where response.data[0] is undefined Also adds some debug logging --- src/cronjobs/processSubscriptions.ts | 3 ++- src/twitch.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cronjobs/processSubscriptions.ts b/src/cronjobs/processSubscriptions.ts index 980e69b..5c53a10 100644 --- a/src/cronjobs/processSubscriptions.ts +++ b/src/cronjobs/processSubscriptions.ts @@ -35,9 +35,9 @@ export async function processSubscriptions(streamer: twitch.StreamerUserIdTokenP var oldFirstUserid = response.data[0].user_id; response = await twitch.streamerApiRequest(streamer, `/subscriptions?broadcaster_id=${streamer.userid}&after=${response.pagination.cursor}&first=100`); - if (oldFirstUserid == response.data[0].user_id) break; log(LogLevel.DEBUG,"Subscription API response:"); log(LogLevel.DEBUG,JSON.stringify(response,null,2)); + if (response.data.length === 0 || oldFirstUserid === response.data[0].user_id) break; } else { break; } @@ -45,6 +45,7 @@ export async function processSubscriptions(streamer: twitch.StreamerUserIdTokenP await dbconn.query("CALL update_scores()"); await dbconn.query('COMMIT'); } catch (e) { + log(LogLevel.ERROR,"cronjobs.processSubscriptions: Exception: " + e); log(LogLevel.ERROR,"cronjobs.processSubscriptions: Exception thrown; rolling back"); await dbconn.query('ROLLBACK'); throw(e); diff --git a/src/twitch.ts b/src/twitch.ts index 0569bee..494bc1a 100644 --- a/src/twitch.ts +++ b/src/twitch.ts @@ -53,6 +53,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise < return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers }) .then(async (res: FetchResponse) => { if (res.status == 200) { + log(LogLevel.DEBUG,"twitch.apiRequest: Request returned 200 for " + endpoint); return res.json(); } else { log(LogLevel.WARNING,"twitch.apiRequest: Failed API request (pre-refresh):");