Skip to content

Commit

Permalink
catches any exceptions while parsing some JSON responses
Browse files Browse the repository at this point in the history
backend may return N/A when retrieving the list of issues fixed in staging/release notes; blindly trying to parse it as JSON throws an exception which

is preventing the rest of a setup method to run. Now we catch/log it.
  • Loading branch information
adrianoc committed Jul 20, 2023
1 parent b8249a9 commit d60a24b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
17 changes: 15 additions & 2 deletions Cecilifier.Web/wwwroot/js/cecilifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,21 @@ function retrieveListOfFixedIssuesInStagingServer(callback) {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
let issues = JSON.parse(this.responseText);
callback(issues);
try {
let issues = JSON.parse(this.responseText);
callback(issues);
}
catch (e) {
SnackBar({
message: `Unable to retrieve list of fixed issues in staging server.`,
dismissible: true,
status: "Warning",
timeout: 120000,
icon: "exclamation"
});

console.log(`Unable to retrieve list of fixed issues in staging server: ${e}\nJSON:\n${this.responseText}`);
}
}
else if (this.readyState === 4 && this.status === 500) {
SnackBar({
Expand Down
45 changes: 29 additions & 16 deletions Cecilifier.Web/wwwroot/js/release_notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,39 @@ function showReleaseNotes() {
const c = getCookie("lastVersion");
const firstTime = c ? new Date(c) : new Date(0);

const json = JSON.parse(text);
const itemsToShow = json.filter(function (item, index, array) {
return new Date(item.published_at) > firstTime;
});
try {
const json = JSON.parse(text);
const itemsToShow = json.filter(function (item, index, array) {
return new Date(item.published_at) > firstTime;
});

if (itemsToShow.length === 0)
return;
if (itemsToShow.length === 0)
return;

setCookie("lastVersion", itemsToShow[0].published_at, 1000);
setCookie("lastVersion", itemsToShow[0].published_at, 1000);

const latest = itemsToShow[0];
const html = `New version <a style='color:#8cbc13; text-decoration: underline' href='${latest.html_url}' target="releaseNotes">${latest.tag_name} ${latest.name}</a> has been released on ${new Date(latest.published_at).toLocaleString()}.<br /><br />${latest.body.replace(/\r\n/g, "<br/>")}`;
const latest = itemsToShow[0];
const html = `New version <a style='color:#8cbc13; text-decoration: underline' href='${latest.html_url}' target="releaseNotes">${latest.tag_name} ${latest.name}</a> has been released on ${new Date(latest.published_at).toLocaleString()}.<br /><br />${latest.body.replace(/\r\n/g, "<br/>")}`;

SnackBar({
message: html,
dismissible: true,
status: "Info",
timeout: 120000,
icon: "exclamation"
});
SnackBar({
message: html,
dismissible: true,
status: "Info",
timeout: 120000,
icon: "exclamation"
});
}
catch (e) {
SnackBar({
message: `Error retrieving release notes.`,
dismissible: true,
status: "Warning",
timeout: 120000,
icon: "exclamation"
});

console.log(`Unable to retrieve release notes: ${e}\nJSON:\n${text}`);
}
});
}

Expand Down

0 comments on commit d60a24b

Please sign in to comment.