fix : now can catch any errors thrown by awaiting req.json()

This commit is contained in:
daragh 2024-01-01 17:16:10 +00:00
parent 7100dad705
commit 7c2a3e6414

View file

@ -1,28 +1,22 @@
export async function post_request(url, body, form_status) {
let req;
try {
req = await fetch(url, {
const req = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
mode: "cors"
});
} catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
return;
}
if (req.status !== 200) {
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
}
let temp = await req.json();
if (temp.result === 'error') {
form_status.innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
} catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
}
}