Bug 34448: Update the way we handle response in http-client.js
Test plan: Before patch: - Visit a non-existent ID i.e. /cgi-bin/koha/erm/agreements/999 - Visit a char ID i.e. /cgi-bin/koha/erm/agreements/abc Apply patch: Repeat above steps. Run cypress tests Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
0312acc211
commit
1b1b5cda5b
1 changed files with 17 additions and 19 deletions
|
@ -21,16 +21,24 @@ class HttpClient {
|
|||
...options,
|
||||
headers: { ...this._headers, ...headers },
|
||||
})
|
||||
.then(response => this.checkError(response, return_response))
|
||||
.then(
|
||||
result => {
|
||||
res = result;
|
||||
},
|
||||
err => {
|
||||
error = err;
|
||||
setError(err.toString());
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
return response.text().then(text => {
|
||||
let json = JSON.parse(text);
|
||||
let message =
|
||||
json.error ||
|
||||
json.errors.map(e => e.message).join("\n") ||
|
||||
json;
|
||||
console.log("Server returned an error:");
|
||||
console.log(response);
|
||||
throw new Error(message);
|
||||
});
|
||||
}
|
||||
)
|
||||
return return_response ? response : response.json();
|
||||
})
|
||||
.then(result => {
|
||||
res = result;
|
||||
})
|
||||
.catch(err => {
|
||||
error = err;
|
||||
setError(err);
|
||||
|
@ -144,16 +152,6 @@ class HttpClient {
|
|||
method: "PATCH",
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default HttpClient;
|
||||
|
|
Loading…
Reference in a new issue