Merge pull request #51 from ThomasRubini/client_error-handling

This commit is contained in:
Thomas Rubini 2023-01-16 09:15:26 +01:00 committed by GitHub
commit c11985edbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,28 +1,32 @@
async function makeAPIRequest(endpoint, body){ async function makeAPIRequest(endpoint, body) {
return new Promise((resolve, reject)=>{ return new Promise((resolve, reject) => {
const fetchOptions = { const fetchOptions = {
method: "POST", method: "POST",
body: new URLSearchParams(body) body: new URLSearchParams(body)
} };
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
resp.json().then(jsonResp=>{ fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
if(jsonResp["error"] == 0){ const responseCode = response.status;
resolve(jsonResp) console.log(responseCode);
}else{ if (responseCode >= 500) {
reject(endpoint+": "+jsonResp["msg"]) reject("Error " + responseCode + " when fetching " + endpoint);
alert("Une réponse invalide du serveur a été obtenue, veuillez réessayer ultérieurement.");
return;
}
response.json().then(jsonResponse => {
if (jsonResponse["error"] === 0) {
resolve(jsonResponse);
} else {
const message = jsonResponse["msg"];
alert("Erreur du serveur : " + message);
reject(endpoint + ": " + message);
} }
}); });
}) }).catch((e) => {
}) console.error("Failed to fetch API", e);
} alert("Une erreur est survenue lors de la connexion au serveur, veuillez vérifier votre connexion et / ou réessayer ultérieurement.");
async function makeAPIImageRequest(endpoint, body){ reject(endpoint);
return new Promise((resolve, reject)=>{ });
const fetchOptions = { });
method: "POST",
body: new URLSearchParams(body)
}
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
resolve(resp)
})
})
} }