我目前正在构建一个用于文件备份的应用程序,它对文件系统进行了大量的读写操作。大部分功能都很好,但我在应用程序的错误处理方面遇到了一些困难。
在下面的屏幕截图中,最后一个路径不是有效的目录,并返回异常,如您所见。
![enter image description here](https://i.stack.imgur.com/oBid4.jpg)
function getTotalSize(pathToDir, dir) {
fs.readdir(pathToDir, function(err, files) {
if (err) {
// handle my error here
throw new Error('something bad happened');
return;
}
// continue if no errors :)
我的问题是,是否可以用我自己的窗口替换标准错误窗口?或者在某些情况下忽略弹出的错误窗口?第一次使用 Electron 很抱歉,如果这是一个明显的问题。
Thanks!
当你抛出错误时readdir
它被高层抓住了uncaughtException
处理程序,由第一行指示:“未捕获的异常”。
您需要做的是添加您自己的自定义处理程序uncaughtException在您的主流程中并显示您想要的任何对话框。
看看dialog module.
例如,您可以使用dialog.showMessageBox
方法来配置有关错误对话框的各种内容,如下所示:
process.on("uncaughtException", (err) => {
const messageBoxOptions = {
type: "error",
title: "Error in Main process",
message: "Something failed"
};
dialog.showMessageBoxSync(messageBoxOptions);
// I believe it used to be the case that doing a "throw err;" here would
// terminate the process, but now it appears that you have to use's Electron's
// app module to exit (process.exit(1) seems to not terminate the process)
app.exit(1);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)