Skip to content

Commit 2d33e1d

Browse files
authored
chore: throw error when response in fetchJson() is not json (@nadalaba) (monkeytypegame#6459)
because vite dev server does not throw a 404 when fetching a non existing json document, but responds with an html document and a 200 status code
1 parent bf0ce66 commit 2d33e1d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

frontend/src/ts/controllers/quotes-controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ class QuotesController {
6565
`quotes/${normalizedLanguage}.json`
6666
);
6767
} catch (e) {
68-
if (e instanceof Error && e?.message?.includes("404")) {
68+
if (
69+
e instanceof Error &&
70+
(e?.message?.includes("404") ||
71+
e?.message?.includes("Content is not JSON"))
72+
) {
6973
return defaultQuoteCollection;
7074
} else {
7175
throw e;

frontend/src/ts/utils/json-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ async function fetchJson<T>(url: string): Promise<T> {
1212
if (!url) throw new Error("No URL");
1313
const res = await fetch(url);
1414
if (res.ok) {
15+
if (res.headers.get("content-type") !== "application/json") {
16+
throw new Error("Content is not JSON");
17+
}
1518
return (await res.json()) as T;
1619
} else {
1720
throw new Error(`${res.status} ${res.statusText}`);

0 commit comments

Comments
 (0)