[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){
|
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(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;
|
||||||
}
|
}
|
||||||
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
|
|
||||||
resp.json().then(jsonResp=>{
|
response.json().then(jsonResponse => {
|
||||||
if(jsonResp["error"] == 0){
|
if (jsonResponse["error"] === 0) {
|
||||||
resolve(jsonResp)
|
resolve(jsonResponse);
|
||||||
}else{
|
} else {
|
||||||
reject(endpoint+": "+jsonResp["msg"])
|
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)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user