Skip to content

Commit

Permalink
Fix axios error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayyy committed Nov 7, 2024
1 parent 826d49b commit 01c3062
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/utils/innerTubeAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import axios, { AxiosError } from "axios";
import { Logger } from "./logger";
import { innerTubeVideoDetails } from "../types/innerTubeApi.model";
import DiskCache from "./diskCache";
Expand Down Expand Up @@ -30,19 +30,29 @@ const privateResponse = (videoId: string, reason: string): innerTubeVideoDetails

export async function getFromITube (videoID: string): Promise<innerTubeVideoDetails> {
if (config.youTubeKeys.floatieUrl) {
const result = await axios.get(config.youTubeKeys.floatieUrl, {
params: {
videoID,
auth: config.youTubeKeys.floatieAuth
try {
const result = await axios.get(config.youTubeKeys.floatieUrl, {
params: {
videoID,
auth: config.youTubeKeys.floatieAuth
}
});

if (result.status === 200) {
return result.data?.videoDetails ?? privateResponse(videoID, result.data?.playabilityStatus?.reason ?? "Bad response");
} else {
return Promise.reject(`Floatie returned non-200 response: ${result.status}`);
}
});
} catch (e) {
if (e instanceof AxiosError) {
const result = e.response;

if (result.status === 200) {
return result.data?.videoDetails ?? privateResponse(videoID, result.data?.playabilityStatus?.reason ?? "Bad response");
} else if (result.status === 500) {
return privateResponse(videoID, result.data ?? "Bad response");
} else {
return Promise.reject(`Floatie returned non-200 response: ${result.status}`);
if (result.status === 500) {
return privateResponse(videoID, result.data ?? "Bad response");
} else {
return Promise.reject(`Floatie returned non-200 response: ${result.status}`);
}
}
}
}

Expand Down

0 comments on commit 01c3062

Please sign in to comment.