Bug 35199: (bug 34448 follow-up) Fix error handling in http-client.js
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 31 Oct 2023 13:45:08 +0000 (14:45 +0100)
committerFridolin Somers <fridolin.somers@biblibre.com>
Thu, 2 Nov 2023 20:36:33 +0000 (10:36 -1000)
commitae3804270d09e19be26e853e349f37de4e7b1c62
treee1451030bcbc54d6f65c08942864c552d55caa95
parent34d08324fcc20c8c83bbbb87f0f1eff0481638f5
Bug 35199: (bug 34448 follow-up) Fix error handling in http-client.js

From bug bug 34448.

386         // Submit the form, get 500
387         cy.intercept("POST", "/api/v1/erm/agreements", {
388             statusCode: 500,
389             error: "Something went wrong",
390         });
391         cy.get("#agreements_add").contains("Submit").click();
392         cy.get("main div[class='dialog alert']").contains(
393             "Something went wrong: SyntaxError: Unexpected end of JSON input"
394         );

This is wrong: we are now showing a JS error (SyntaxError) instead of the expected 500: internal server error!

The problem was that a regular 500 does not have anything in the body,
and _fetchJSON didn't handle that ( JSON.parse(text) ).
If the body of the response does not contain anything we need to get the
text from statusText (which contains "Internal Server Error" in case of
500).

Test plan:
1. Make sure all cypress tests pass
2. Confirm the above:
Raise an exception from a given route (/agreements for instance) and
confirm that the error displayed on the interface is correct (ie. not
SyntaxError, but "Error: Internal Server Error")

For QA:
* This change is covered properly in Dialog_spec.ts, no need to redo it in
every other test files.
* Without the following change in count, we see:
"Something went wrong: Error: Error: Internal Server Error" because
setError is called twice. We don't need to set the error from count, it
has been set from _fetchJSON already.
-            error => {
-                setError(error.toString());
-            }
+            error => {}

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit df3acf3d68cd24d3b1015432206236b563aaa5d1)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js
t/cypress/integration/ERM/Agreements_spec.ts
t/cypress/integration/ERM/Dialog_spec.ts
t/cypress/integration/ERM/Licenses_spec.ts
t/cypress/integration/ERM/Packages_spec.ts
t/cypress/integration/ERM/Titles_spec.ts