From 5da2e786f3016cf81630a41fe53c919c1a1a11ca Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 15 Feb 2023 10:36:38 +0100 Subject: [PATCH] Bug 32939: Restore checkError --- .../prog/js/vue/fetch/http-client.js | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js index 63731a5657a..d1b5a917b9e 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js @@ -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 } -- 2.25.1