Integrated error handling into the RequestUtil.js

This commit is contained in:
Mathias Wagner 2024-01-01 12:31:33 +01:00
parent 4bd0e1b72b
commit 0b93f024ec
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -9,11 +9,18 @@ export const request = async (path, method = "GET", body = {}, headers = {}, abo
if (abort) setTimeout(() => {controller.abort()}, 10000); if (abort) setTimeout(() => {controller.abort()}, 10000);
return await fetch("/api" + path, { const result = await fetch("/api" + path, {
headers: {"Content-Type": "application/json", ...getHeaders(), ...headers}, method, headers: {"Content-Type": "application/json", ...getHeaders(), ...headers}, method,
body: method !== "GET" ? JSON.stringify(body) : null, body: method !== "GET" ? JSON.stringify(body) : null,
signal: controller.signal signal: controller.signal
}); });
if (result.status === 401) {
localStorage.removeItem("token");
throw new Error("Unauthorized");
}
return result;
} }
// Run a GET request and get the json of the response // Run a GET request and get the json of the response