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:
parent
ef7da71ad5
commit
b995adeb4d
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue