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
master
Dessa Simpson 2022-02-28 09:34:18 -07:00
parent ac953b0bb5
commit dd423bdb34
2 changed files with 3 additions and 1 deletions

View File

@ -35,9 +35,9 @@ export async function processSubscriptions(streamer: twitch.StreamerUserIdTokenP
var oldFirstUserid = response.data[0].user_id; var oldFirstUserid = response.data[0].user_id;
response = await twitch.streamerApiRequest(streamer, response = await twitch.streamerApiRequest(streamer,
`/subscriptions?broadcaster_id=${streamer.userid}&after=${response.pagination.cursor}&first=100`); `/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,"Subscription API response:");
log(LogLevel.DEBUG,JSON.stringify(response,null,2)); log(LogLevel.DEBUG,JSON.stringify(response,null,2));
if (response.data.length === 0 || oldFirstUserid === response.data[0].user_id) break;
} else { } else {
break; break;
} }
@ -45,6 +45,7 @@ export async function processSubscriptions(streamer: twitch.StreamerUserIdTokenP
await dbconn.query("CALL update_scores()"); await dbconn.query("CALL update_scores()");
await dbconn.query('COMMIT'); await dbconn.query('COMMIT');
} catch (e) { } catch (e) {
log(LogLevel.ERROR,"cronjobs.processSubscriptions: Exception: " + e);
log(LogLevel.ERROR,"cronjobs.processSubscriptions: Exception thrown; rolling back"); log(LogLevel.ERROR,"cronjobs.processSubscriptions: Exception thrown; rolling back");
await dbconn.query('ROLLBACK'); await dbconn.query('ROLLBACK');
throw(e); throw(e);

View File

@ -53,6 +53,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers }) return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers })
.then(async (res: FetchResponse) => { .then(async (res: FetchResponse) => {
if (res.status == 200) { if (res.status == 200) {
log(LogLevel.DEBUG,"twitch.apiRequest: Request returned 200 for " + endpoint);
return res.json(); return res.json();
} else { } else {
log(LogLevel.WARNING,"twitch.apiRequest: Failed API request (pre-refresh):"); log(LogLevel.WARNING,"twitch.apiRequest: Failed API request (pre-refresh):");