From be491f28e3a93ec31faf4fd93acfd038a2d0ac30 Mon Sep 17 00:00:00 2001 From: Marc Horlacher Date: Fri, 1 Nov 2024 01:02:25 +0100 Subject: [PATCH] added alternative query of gist content from raw URL if initial fetch fails --- src/fileSystem/api.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/fileSystem/api.ts b/src/fileSystem/api.ts index 60f85d6..251290e 100644 --- a/src/fileSystem/api.ts +++ b/src/fileSystem/api.ts @@ -8,14 +8,28 @@ const isBinaryPath = require("is-binary-path"); export async function getFileContents(file: GistFile) { if (file.truncated || !file.content) { const responseType = isBinaryPath(file.filename!) ? "arraybuffer" : "text"; - const { data } = await axios.get(file.raw_url!, { - responseType, - transformResponse: (data) => { - return data; + try { + const { data } = await axios.get(file.raw_url!, { + responseType, + transformResponse: (data) => { + return data; + } + }); + file.content = data; + } catch (error: any) { + console.error(`Error fetching file content: ${(error as Error).message}`); + // Fallback: try to fetch content directly from raw_url + try { + const response = await fetch(file.raw_url!); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + file.content = await response.text(); + } catch (fallbackError: any) { + console.error(`Fallback fetch failed: ${(fallbackError as Error).message}`); + throw new Error(`Failed to fetch file content: ${fallbackError.message}`); } - }); - - file.content = data; + } } return file.content!; @@ -52,4 +66,4 @@ export async function updateGistFiles( }); return gist; -} +} \ No newline at end of file