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){
return new Promise((resolve, reject)=>{
async function makeAPIRequest(endpoint, body) {
return new Promise((resolve, reject) => {
const fetchOptions = {
method: "POST",
body: new URLSearchParams(body)
}
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
resp.json().then(jsonResp=>{
if(jsonResp["error"] == 0){
resolve(jsonResp)
}else{
reject(endpoint+": "+jsonResp["msg"])
};
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
const responseCode = response.status;
console.log(responseCode);
if (responseCode >= 500) {
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);
}
});
})
})
}
async function makeAPIImageRequest(endpoint, body){
return new Promise((resolve, reject)=>{
const fetchOptions = {
method: "POST",
body: new URLSearchParams(body)
}
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
resolve(resp)
})
})
}).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.");
reject(endpoint);
});
});
}