feature (cancellation): logic to cancel request in client and server

This commit is contained in:
Mickael Kerjean
2022-09-02 17:24:36 +10:00
parent 0bfab6eff2
commit dd6f0ca407
6 changed files with 28 additions and 9 deletions

View File

@ -1,4 +1,4 @@
export function http_get(url, type = "json") {
export function http_get(url, type = "json", params) {
return new Promise((done, err) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
@ -30,6 +30,12 @@ export function http_get(url, type = "json") {
xhr.onerror = function() {
handle_error_response(xhr, err);
};
if (params && params.abort) {
params.abort.signal.onabort = () => {
xhr.abort();
handle_error_response(xhr, err);
};
}
});
}
@ -152,10 +158,17 @@ function handle_error_response(xhr, err) {
if (navigator.onLine === false) {
err({ message: "Connection Lost", code: "NO_INTERNET" });
} else if (xhr.status === 0 && xhr.responseText === "") {
err({
message: "Service unavailable, if the problem persist, contact your administrator",
code: "INTERNAL_SERVER_ERROR",
});
switch(xhr.readyState) {
case XMLHttpRequest.DONE:
case XMLHttpRequest.UNSENT:
err({ message: "aborted", code: "ABORTED" });
break
default:
err({
message: "Service unavailable, if the problem persist, contact your administrator",
code: "INTERNAL_SERVER_ERROR",
});
}
} else if (xhr.status === 500) {
err({
message: message || "Oups something went wrong with our servers",