Skip to content

Commit

Permalink
fix(twitch:oauth): handle another exception message (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal authored May 21, 2020
1 parent 200522c commit 06039a0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
18 changes: 17 additions & 1 deletion src/twitch/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import * as qs from 'qs';
import { getTwitchApiKey, readFromSyncStorage, writeToSyncStorage } from '..';
import { isAxiosError } from '../utils';

const oAuthUrl = 'https://id.twitch.tv/oauth2';

Expand Down Expand Up @@ -82,3 +83,18 @@ export async function askTwitchAccessToken(force: boolean = false) {

return accessToken;
}

export function isTwitchApiOAuthError(error: Error | AxiosError) {
if (isAxiosError(error)) {
if (
error.response.status === 401 &&
['Invalid OAuth token', 'Client ID and OAuth token do not match'].includes(error.response.data.message)
) {
return true;
}

return false;
}

return false;
}
12 changes: 5 additions & 7 deletions src/twitch/getTwitchGame.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios';
import { askTwitchAccessToken, getTwitchAccessToken, getTwitchApiKey, registerTwitchAccessToken } from '.';
import { isTwitchApiOAuthError } from './authorization';

export interface Game {
id: string;
Expand All @@ -20,14 +21,11 @@ const sendRequest = async (id: string): Promise<Game | null> => {
try {
response = await axios.get(`https://api.twitch.tv/helix/games`, config);
} catch (error) {
if (error.response) {
if (error.response.status === 401 && error.response.data.message === 'Invalid OAuth token') {
registerTwitchAccessToken(await askTwitchAccessToken(true));
return sendRequest(id);
}
} else {
throw error;
if (isTwitchApiOAuthError(error)) {
registerTwitchAccessToken(await askTwitchAccessToken(true));
return sendRequest(id);
}
throw error;
}

return Promise.resolve(response.data.data[0] || null);
Expand Down
12 changes: 5 additions & 7 deletions src/twitch/getTwitchLiveStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
askTwitchAccessToken,
registerTwitchAccessToken,
} from '.';
import { isTwitchApiOAuthError } from './authorization';
import { Game } from './getTwitchGame';

export interface Stream {
Expand Down Expand Up @@ -38,14 +39,11 @@ export const getTwitchLiveStreams = (usersId: number[]): Promise<Payload> => {
try {
response = await axios.get(url, config);
} catch (error) {
if (error.response) {
if (error.response.status === 401 && error.response.data.message === 'Invalid OAuth token') {
registerTwitchAccessToken(await askTwitchAccessToken(true));
return getTwitchLiveStreams(usersId).then(resolve);
}
} else {
throw error;
if (isTwitchApiOAuthError(error)) {
registerTwitchAccessToken(await askTwitchAccessToken(true));
return getTwitchLiveStreams(usersId).then(resolve);
}
throw error;
}

const streams: Stream[] = response.data.data;
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AxiosError } from 'axios';

export function isAxiosError(error: Error | AxiosError): error is AxiosError {
return 'response' in error;
}

0 comments on commit 06039a0

Please sign in to comment.