Skip to content

Commit 1c90412

Browse files
committed
Display error message to user if file or directory isn't selected during export
1 parent d21b7ec commit 1c90412

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

src/modules/flightlog.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export function exportFlightLogToCSVs(flightLog) {
1010
let paths = dialog.showOpenDialogSync({ properties: ["openDirectory"] });
1111

1212
if (!paths) {
13-
console.log("No directory selected for export.");
14-
// TODO: after snackbar is merged print error message to user
15-
return;
13+
throw new Error("No directory selected for export.");
1614
}
1715

1816
const userFolderPath = path.join(paths[0]);
@@ -23,9 +21,7 @@ export function exportFlightLogToCSVs(flightLog) {
2321
for (let flightLogSection of flightLogSections) {
2422
fs.writeFile(`${exportFolderPath}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) {
2523
if (err) {
26-
console.log("An error occurred while writing CSV object to file.");
27-
// TODO: after snackbar is merged print error message to user
28-
return console.log(err);
24+
throw new Error(`An error occurred while writing CSV object to file.`);
2925
}
3026
});
3127
}
@@ -35,9 +31,7 @@ export function exportFlightLogChartsToHTML(flightLogChartsHTML) {
3531
let paths = dialog.showOpenDialogSync({ properties: ["openDirectory"] });
3632

3733
if (!paths) {
38-
console.log("No directory selected for export.");
39-
return;
40-
// TODO: after snackbar is merged print error message to user
34+
throw new Error("No directory selected for export.");
4135
}
4236

4337
const exportFolderPath = path.join(paths[0]);
@@ -56,9 +50,7 @@ export function exportFlightLogChartsToHTML(flightLogChartsHTML) {
5650

5751
fs.writeFile(`${exportFolderPath}/${flightLogFilename}_plots_${formatDateTime(new Date())}.html`, flightLogHtmlDocument, 'utf8', function (err) {
5852
if (err) {
59-
console.log("An error occurred while writing HTML Object to File.");
60-
return console.log(err);
61-
// TODO: after snackbar is merged print error message to user
53+
throw new Error(`An error occurred while writing HTML Object to File.`);
6254
}
6355
});
6456
}

src/modules/ipc.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,21 @@ export function subscribeListeners() {
7575
});
7676

7777
ipcMain.on("EXPORT_FLIGHTLOG_CSVS", (event, flightLog) => {
78-
exportFlightLogToCSVs(flightLog)
79-
event.sender.send("EXPORT_FLIGHTLOG_CSVS");
78+
try {
79+
exportFlightLogToCSVs(flightLog)
80+
event.sender.send("EXPORT_FLIGHTLOG_CSVS");
81+
} catch (error) {
82+
event.sender.send("EXPORT_FLIGHTLOG_CSVS", { error: error.message });
83+
}
8084
});
8185

8286
ipcMain.on("EXPORT_FLIGHTLOG_HTML", (event, flightLogChartsHTML) => {
83-
exportFlightLogChartsToHTML(flightLogChartsHTML);
84-
event.sender.send("EXPORT_FLIGHTLOG_HTML");
87+
try {
88+
exportFlightLogChartsToHTML(flightLogChartsHTML);
89+
event.sender.send("EXPORT_FLIGHTLOG_HTML");
90+
} catch (error) {
91+
event.sender.send("EXPORT_FLIGHTLOG_HTML", { error: error.message });
92+
}
8593
});
8694

8795
ipcMain.on("BOARD:RESET_CONFIG", () => {

src/modules/serial.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ function onData(data) {
165165
if (currentCommand === "dump") {
166166
if (data === "#Configuration dump") return;
167167
else if (data === "#End of configuration dump") {
168-
saveDumpDataToFile(backupConfig.trim());
169-
sendToRenderer("BOARD:DUMP");
168+
try {
169+
saveDumpDataToFile(backupConfig.trim());
170+
sendToRenderer("BOARD:DUMP");
171+
} catch (error) {
172+
return sendToRenderer("BOARD:DUMP", { error: error.message });
173+
}
170174
backupConfig = "";
171175
} else backupConfig += data + "\n";
172176

@@ -294,6 +298,8 @@ function saveDumpDataToFile(data) {
294298
if (paths) {
295299
const file = path.join(paths[0], "backup_cats_config.txt");
296300
fs.writeFileSync(file, data);
301+
} else {
302+
throw new Error("No directory selected for backup.");
297303
}
298304
}
299305

src/store/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export default new Vuex.Store({
142142
showSuccessSnackbar({ commit }, message) {
143143
commit("SHOW_SNACKBAR", { message, color: "success" });
144144
},
145+
showErrorSnackbar({ commit }, message) {
146+
commit("SHOW_SNACKBAR", { message, color: "error" });
147+
},
145148
hideSnackbar({ commit }) {
146149
commit("HIDE_SNACKBAR");
147150
},

src/views/Config.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,14 @@ export default {
419419
},
420420
mounted() {
421421
this.init();
422-
window.renderer.on("BOARD:DUMP", () => {
422+
window.renderer.on("BOARD:DUMP", (result) => {
423423
this.backupLoading = false
424-
this.showSuccessSnackbar("Backup created!");
424+
if (result?.error) {
425+
this.showErrorSnackbar(`${result.error}`);
426+
return;
427+
} else {
428+
this.showSuccessSnackbar("Backup created!");
429+
}
425430
});
426431
window.renderer.on("BOARD:RESTORE", () => {
427432
this.restoreLoading = false;
@@ -441,7 +446,7 @@ export default {
441446
clearInterval(this.timer);
442447
},
443448
methods: {
444-
...mapActions(["setChangedTab", "showSuccessSnackbar"]),
449+
...mapActions(["setChangedTab", "showSuccessSnackbar", "showErrorSnackbar"]),
445450
init() {
446451
getConfigs();
447452
this.getInfo();

src/views/Home.vue

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,27 @@ export default {
106106
107107
if (el) makePlots(flightLog, el, this.useImperialUnits);
108108
});
109-
window.renderer.on("EXPORT_FLIGHTLOG_CSVS", (flightLog) => {
109+
window.renderer.on("EXPORT_FLIGHTLOG_CSVS", (result) => {
110110
this.exportButtonLoading = false;
111-
this.showSuccessSnackbar("Flight log CSVs exported!");
111+
if (result?.error) {
112+
this.showErrorSnackbar(`${result.error}`);
113+
return;
114+
} else {
115+
this.showSuccessSnackbar("Flight log CSVs exported!");
116+
}
112117
});
113-
window.renderer.on("EXPORT_FLIGHTLOG_HTML", (flightLog) => {
118+
window.renderer.on("EXPORT_FLIGHTLOG_HTML", (result) => {
114119
this.exportButtonLoading = false;
115-
this.showSuccessSnackbar("Flight log HTML plots exported!");
120+
if (result?.error) {
121+
this.showErrorSnackbar(`${result.error}`);
122+
return;
123+
} else {
124+
this.showSuccessSnackbar("Flight log HTML plots exported!");
125+
}
116126
});
117127
},
118128
methods: {
119-
...mapActions(["showSuccessSnackbar"]),
129+
...mapActions(["showSuccessSnackbar", "showErrorSnackbar"]),
120130
loadFlightLog(file) {
121131
this.loadButtonLoading = true;
122132
this.flightLog = null

0 commit comments

Comments
 (0)