chore: throw error when response in fetchJson() is not json (@NadAlaba) (#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
This commit is contained in:
Nad Alaba 2025-04-16 22:31:56 +03:00 committed by GitHub
parent bf0ce6607d
commit 2d33e1da41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -65,7 +65,11 @@ class QuotesController {
`quotes/${normalizedLanguage}.json`
);
} catch (e) {
if (e instanceof Error && e?.message?.includes("404")) {
if (
e instanceof Error &&
(e?.message?.includes("404") ||
e?.message?.includes("Content is not JSON"))
) {
return defaultQuoteCollection;
} else {
throw e;

View file

@ -12,6 +12,9 @@ async function fetchJson<T>(url: string): Promise<T> {
if (!url) throw new Error("No URL");
const res = await fetch(url);
if (res.ok) {
if (res.headers.get("content-type") !== "application/json") {
throw new Error("Content is not JSON");
}
return (await res.json()) as T;
} else {
throw new Error(`${res.status} ${res.statusText}`);