[Client] Handle connexion errors and invalid API responses
An alert is shown to the user and errors are logged to the console. Also reformat the function code style.
This commit is contained in:
parent
bd84671098
commit
b1e8796640
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user