Skip to content

Commit 84d2da9

Browse files
committed
fix: better error messages Issue #40, partially addressing Issue #54
1 parent d4a0c0a commit 84d2da9

File tree

1 file changed

+119
-59
lines changed

1 file changed

+119
-59
lines changed

html/classhelper.js

Lines changed: 119 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,32 @@ class ClassHelper extends HTMLElement {
277277
url.searchParams.append("@template", "json");
278278
url.searchParams.append("properties", Object.keys(translations).join(','));
279279

280-
let resp;
280+
let resp, json;
281+
281282
try {
282283
resp = await fetch(url);
283-
if (!resp.ok) throw new Error("Not 2xx status code.", { cause: resp });
284284
} catch (error) {
285-
throw new Error("error fetching translations from roundup rest api", { cause: error });
285+
let message = `Error fetching translations from roundup rest api\n`;
286+
message += `url: ${url.toString()}\n`;
287+
throw new Error(message, { cause: error });
286288
}
287289

288290
try {
289-
ClassHelper.translations = await resp.json();
291+
json = await resp.json();
290292
} catch (error) {
291-
throw new Error("error parsing translation json from roundup rest api", { cause: error });
293+
let message = `Error parsing json from roundup rest api\n`;
294+
message += `url: ${url.toString()}\n`;
295+
throw new Error(message, { cause: error });
292296
}
297+
298+
if (!resp.ok) {
299+
let message = `Unexpected response\n`;
300+
message += `url: ${url.toString()}\n`;
301+
message += `response status: ${resp.status}\n`;
302+
throw new Error(message, { cause: json });
303+
}
304+
305+
ClassHelper.translations = translations;
293306
}
294307

295308
async fetchDropdownsData() {
@@ -323,19 +336,28 @@ class ClassHelper extends HTMLElement {
323336
url += `&@sort=${sortOrder}`;
324337
}
325338

326-
let resp;
339+
let resp, json;
327340
try {
328341
resp = await fetch(url);
329-
if (!resp.ok) throw new Error("Not 2xx status code.", { cause: resp });
330342
} catch (error) {
331-
throw new Error("error fetching dropdowns from roundup rest api", { cause: error });
343+
let message = `Error fetching translations from roundup rest api\n`;
344+
message += `url: ${url.toString()}\n`;
345+
throw new Error(message, { cause: error });
332346
}
333347

334-
let json;
335348
try {
336349
json = await resp.json();
337350
} catch (error) {
338-
throw new Error("error parsing dropdown json from roundup rest api");
351+
let message = `Error parsing json from roundup rest api\n`;
352+
message += `url: ${url.toString()}\n`;
353+
throw new Error(message, { cause: error });
354+
}
355+
356+
if (!resp.ok) {
357+
let message = `Unexpected response\n`;
358+
message += `url: ${url.toString()}\n`;
359+
message += `response status: ${resp.status}\n`;
360+
throw new Error(message, { cause: json });
339361
}
340362

341363
let list = new Map();
@@ -345,6 +367,8 @@ class ClassHelper extends HTMLElement {
345367
let valueKey = Object.keys(json.data.collection[0]).find(key => key !== "id" && key !== "link");
346368

347369
if (!valueKey) {
370+
let message = `No suitable key found for value in dropdown data\n`;
371+
message += `url: ${url.toString()}\n`;
348372
throw new Error("No value key found in dropdown data for: " + url);
349373
}
350374

@@ -810,7 +834,7 @@ class ClassHelper extends HTMLElement {
810834
async openPopUp(apiURL, props) {
811835

812836
/** @type {Response} */
813-
let resp;
837+
let resp, json;
814838
/** @type {any} */
815839
let collection;
816840
/** @type {string} */
@@ -825,30 +849,34 @@ class ClassHelper extends HTMLElement {
825849
} catch (error) {
826850
let message = `Error fetching data from roundup rest api`;
827851
message += `url: ${apiURL.toString()}\n`;
828-
if (resp?.status) {
829-
message += `response status: ${resp.status}\n`;
830-
}
831852
throw new Error(message, { cause: error });
832853
}
833854

834855
try {
835-
const json = await resp.json();
836-
collection = json.data.collection;
837-
838-
const links = json.data["@links"];
839-
if (links?.prev?.length > 0) {
840-
prevPageURL = links.prev[0].uri;
841-
}
842-
if (links?.next?.length > 0) {
843-
nextPageURL = links.next[0].uri;
844-
}
856+
json = await resp.json();
845857
} catch (error) {
846858
let message = "Error parsing json from roundup rest api\n";
847859
message += `url: ${apiURL.toString()}\n`;
848-
message += `response status: ${resp.status}`;
849860
throw new Error(message, { cause: error });
850861
}
851862

863+
if (!resp.ok) {
864+
let message = `Unexpected response\n`;
865+
message += `url: ${url.toString()}\n`;
866+
message += `response status: ${resp.status}\n`;
867+
throw new Error(message, { cause: json });
868+
}
869+
870+
collection = json.data.collection;
871+
872+
const links = json.data["@links"];
873+
if (links?.prev?.length > 0) {
874+
prevPageURL = links.prev[0].uri;
875+
}
876+
if (links?.next?.length > 0) {
877+
nextPageURL = links.next[0].uri;
878+
}
879+
852880
if (props.formProperty) {
853881
// Find preselected values
854882
const input = document.getElementsByName(props.formProperty).item(0);
@@ -956,40 +984,56 @@ class ClassHelper extends HTMLElement {
956984
* @throws {Error} when fetching or parsing data from roundup rest api fails
957985
*/
958986
async pageChange(apiURL, props) {
959-
let resp;
987+
988+
/** @type {Response} */
989+
let resp, json;
990+
/** @type {any} */
991+
let collection;
992+
/** @type {string} */
993+
let prevPageURL;
994+
/** @type {string} */
995+
let nextPageURL;
996+
/** @type {URL} */
997+
let selfPageURL;
998+
/** @type {string[]} */
999+
let accumulatorValues = [];
1000+
9601001
try {
9611002
resp = await fetch(apiURL);
9621003
} catch (error) {
963-
// Show message fail to load data
964-
throw new Error("error fetching data from roundup rest api");
1004+
let message = `Error fetching data from roundup rest api`;
1005+
message += `url: ${apiURL.toString()}\n`;
1006+
throw new Error(message, { cause: error });
9651007
}
9661008

967-
let json;
9681009
try {
9691010
json = await resp.json();
9701011
} catch (error) {
971-
// Show message fail to parse json
972-
throw new Error("error parsing json from roundup rest api");
1012+
let message = "Error parsing json from roundup rest api\n";
1013+
message += `url: ${apiURL.toString()}\n`;
1014+
throw new Error(message, { cause: error });
9731015
}
9741016

975-
const data = json.data;
976-
const links = json.data["@links"];
1017+
if (!resp.ok) {
1018+
let message = `Unexpected response\n`;
1019+
message += `url: ${url.toString()}\n`;
1020+
message += `response status: ${resp.status}\n`;
1021+
throw new Error(message, { cause: json });
1022+
}
9771023

978-
let prevPageURL, nextPageURL, selfPageURL;
1024+
collection = json.data.collection;
9791025

980-
if (links.prev && links.prev.length > 0) {
1026+
const links = json.data["@links"];
1027+
if (links?.prev?.length > 0) {
9811028
prevPageURL = links.prev[0].uri;
9821029
}
983-
if (links.next && links.next.length > 0) {
1030+
if (links?.next?.length > 0) {
9841031
nextPageURL = links.next[0].uri;
9851032
}
986-
if (links.self && links.self.length > 0) {
1033+
if (links?.self?.length > 0) {
9871034
selfPageURL = new URL(links.self[0].uri);
9881035
}
9891036

990-
/** @type {string[]} */
991-
let accumulatorValues = [];
992-
9931037
const preview = this.popupRef.document.getElementById("popup-preview");
9941038
if (preview) {
9951039
accumulatorValues = preview.value.split(",");
@@ -1000,11 +1044,11 @@ class ClassHelper extends HTMLElement {
10001044
const pageIndex = selfPageURL.searchParams.get("@page_index");
10011045

10021046
const oldPaginationFrag = popupDocument.getElementById("popup-pagination");
1003-
const newPaginationFrag = this.getPaginationFragment(prevPageURL, nextPageURL, pageIndex, props.pageSize, data.collection.length);
1047+
const newPaginationFrag = this.getPaginationFragment(prevPageURL, nextPageURL, pageIndex, props.pageSize, collection.length);
10041048
popupBody.replaceChild(newPaginationFrag, oldPaginationFrag);
10051049

10061050
let oldTableFrag = popupDocument.getElementById("popup-tablediv");
1007-
let newTableFrag = this.getTableFragment(props.fields, data.collection, accumulatorValues);
1051+
let newTableFrag = this.getTableFragment(props.fields, collection, accumulatorValues);
10081052
popupBody.replaceChild(newTableFrag, oldTableFrag);
10091053
}
10101054

@@ -1028,40 +1072,56 @@ class ClassHelper extends HTMLElement {
10281072
* @throws {Error} when fetching or parsing data from roundup rest api fails
10291073
*/
10301074
async searchEvent(apiURL, props) {
1031-
let resp;
1075+
1076+
/** @type {Response} */
1077+
let resp, json;
1078+
/** @type {any} */
1079+
let collection;
1080+
/** @type {string} */
1081+
let prevPageURL;
1082+
/** @type {string} */
1083+
let nextPageURL;
1084+
/** @type {URL} */
1085+
let selfPageURL;
1086+
/** @type {string[]} */
1087+
let accumulatorValues = [];
1088+
10321089
try {
10331090
resp = await fetch(apiURL);
10341091
} catch (error) {
1035-
// Show message fail to load data
1036-
throw new Error("error fetching data from roundup rest api");
1092+
let message = `Error fetching data from roundup rest api`;
1093+
message += `url: ${apiURL.toString()}\n`;
1094+
throw new Error(message, { cause: error });
10371095
}
10381096

1039-
let json;
10401097
try {
10411098
json = await resp.json();
10421099
} catch (error) {
1043-
// Show message fail to parse json
1044-
throw new Error("error parsing json from roundup rest api");
1100+
let message = "Error parsing json from roundup rest api\n";
1101+
message += `url: ${apiURL.toString()}\n`;
1102+
throw new Error(message, { cause: error });
10451103
}
10461104

1047-
const data = json.data;
1048-
const links = json.data["@links"];
1105+
if (!resp.ok) {
1106+
let message = `Unexpected response\n`;
1107+
message += `url: ${url.toString()}\n`;
1108+
message += `response status: ${resp.status}\n`;
1109+
throw new Error(message, { cause: json });
1110+
}
10491111

1050-
let prevPageURL, nextPageURL, selfPageURL;
1112+
collection = json.data.collection;
10511113

1052-
if (links.prev && links.prev.length > 0) {
1114+
const links = json.data["@links"];
1115+
if (links?.prev?.length > 0) {
10531116
prevPageURL = links.prev[0].uri;
10541117
}
1055-
if (links.next && links.next.length > 0) {
1118+
if (links?.next?.length > 0) {
10561119
nextPageURL = links.next[0].uri;
10571120
}
1058-
if (links.self && links.self.length > 0) {
1121+
if (links?.self?.length > 0) {
10591122
selfPageURL = new URL(links.self[0].uri);
10601123
}
10611124

1062-
/** @type {string[]} */
1063-
let accumulatorValues = [];
1064-
10651125
const preview = this.popupRef.document.getElementById("popup-preview");
10661126
if (preview) {
10671127
accumulatorValues = preview.value.split(",");
@@ -1072,12 +1132,12 @@ class ClassHelper extends HTMLElement {
10721132
const pageIndex = selfPageURL.searchParams.get("@page_index");
10731133

10741134
const oldPaginationFrag = popupDocument.getElementById("popup-pagination");
1075-
let newPaginationFrag = this.getPaginationFragment(prevPageURL, nextPageURL, pageIndex, props.pageSize, data.collection.length);
1135+
let newPaginationFrag = this.getPaginationFragment(prevPageURL, nextPageURL, pageIndex, props.pageSize, collection.length);
10761136
popupBody.replaceChild(newPaginationFrag, oldPaginationFrag);
10771137

10781138

10791139
let oldTableFrag = popupDocument.getElementById("popup-tablediv");
1080-
let newTableFrag = this.getTableFragment(props.fields, data.collection, accumulatorValues);
1140+
let newTableFrag = this.getTableFragment(props.fields, collection, accumulatorValues);
10811141
popupBody.replaceChild(newTableFrag, oldTableFrag);
10821142
}
10831143

0 commit comments

Comments
 (0)