Skip to content

Commit

Permalink
Fix: Handle fetch errors if response code is not 2XX
Browse files Browse the repository at this point in the history
  • Loading branch information
graphemecluster committed Oct 4, 2024
1 parent 28c9878 commit fc0f631
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Components/SchemaEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
: /([^/]*)\/*$/.exec(url.pathname)![1]; // Use the last segment of the path as name
if (url.hostname === "github.com") {
url.searchParams.append("raw", "true"); // Fetch raw file content for GitHub files
} else if (url.hostname === "gist.github.com") {
} else if (url.hostname === "gist.github.com" && !url.pathname.endsWith("/raw")) {
url.pathname += "/raw"; // Fetch raw file content for GitHub gists
}
const response = await fetch(url, {
Expand All @@ -315,6 +315,7 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
cache: "no-cache",
signal,
});
if (!response.ok) throw new Error(await response.text());
const blob = await response.blob();
return new File([blob], name);
}),
Expand Down
5 changes: 3 additions & 2 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ export function notifyError(msg: string, err?: unknown) {

export async function fetchFile(input: string) {
try {
const text = await (await fetch(input, { cache: "no-cache" })).text();
if (text.startsWith("Failed to fetch")) throw new Error(text);
const response = await fetch(input, { cache: "no-cache" });
const text = await response.text();
if (!response.ok) throw new Error(text);
return text;
} catch (err) {
throw notifyError("載入檔案失敗", err);
Expand Down

0 comments on commit fc0f631

Please sign in to comment.