Skip to content

Commit 8bdd002

Browse files
committed
added try-catch clause for better error handling
1 parent 06ef745 commit 8bdd002

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

main.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,13 @@ async function readOmdbApiKeyFromFile() {
215215
console.log("Something went wrong trying to read JSON file.");
216216
}
217217

218-
const jsonContent = JSON.parse(jsonStr);
218+
let jsonContent;
219+
try {
220+
jsonContent = JSON.parse(jsonStr);
221+
} catch (error) {
222+
console.error("There was an error parsing json file.");
223+
return;
224+
}
219225

220226
if (jsonContent && jsonContent.key) {
221227
KEY = jsonContent.key;
@@ -271,7 +277,13 @@ function movieHandler(arg) {
271277
}
272278

273279
try {
274-
const jsonData = JSON.parse(data);
280+
let jsonData;
281+
try {
282+
jsonData = JSON.parse(data);
283+
} catch (error) {
284+
console.error("There was an error parsing json file.");
285+
return;
286+
}
275287

276288
if (opt === "remove") {
277289
// Find the index of the object with the wrong movie id

renderer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,13 @@ function readMoviesFromFile() {
131131
}
132132

133133
try {
134-
const jsonContent = JSON.parse(jsonStr); // movies
134+
let jsonContent; // movies
135+
try {
136+
jsonContent = JSON.parse(jsonStr);
137+
} catch (error) {
138+
console.error("There was an error parsing json file.");
139+
return;
140+
}
135141

136142
setFilters(jsonContent);
137143
listFiltersOnGUI();
@@ -329,7 +335,13 @@ async function readOmdbApiKeyFromFile() {
329335
console.log("Something went wrong trying to read JSON file.");
330336
}
331337

332-
const jsonContent = JSON.parse(jsonStr);
338+
let jsonContent;
339+
try {
340+
jsonContent = JSON.parse(jsonStr);
341+
} catch (error) {
342+
console.error("There was an error parsing json file.");
343+
return;
344+
}
333345

334346
if (jsonContent && jsonContent.key) {
335347
KEY = jsonContent.key;

0 commit comments

Comments
 (0)