Bug 32939: Restore checkError

Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 929f5a2199)
Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
This commit is contained in:
Jonathan Druart 2023-02-15 10:36:38 +01:00 committed by Matt Blenkinsop
parent ef7da71ad5
commit b995adeb4d
1 changed files with 28 additions and 9 deletions

View File

@ -1,3 +1,5 @@
import { setError } from "../messages";
class HttpClient {
constructor(options = {}) {
this._baseURL = options.baseURL || "";
@ -7,17 +9,24 @@ class HttpClient {
}
async _fetchJSON(endpoint, headers = {}, options = {}) {
const res = await fetch(this._baseURL + endpoint, {
let res;
await fetch(this._baseURL + endpoint, {
...options,
headers: { ...this._headers, ...headers },
});
if (!res.ok) throw new Error(res.statusText);
if (options.parseResponse !== false && res.status !== 204)
return res.json();
return undefined;
})
.then(this.checkError)
.then(
(result) => {
res = result;
},
(error) => {
setError(error.toString());
}
)
.catch((error) => {
setError(error);
});
return res;
}
get(params = {}) {
@ -51,6 +60,16 @@ class HttpClient {
});
}
checkError(response, return_response = 0) {
if (response.status >= 200 && response.status <= 299) {
return return_response ? response : response.json();
} else {
console.log("Server returned an error:");
console.log(response);
throw Error("%s (%s)".format(response.statusText, response.status));
}
}
//TODO: Implement count method
}