Javascript Electron中的自定义错误窗口/处理

Javascript Electron中的自定义错误窗口/处理,javascript,node.js,electron,fs,Javascript,Node.js,Electron,Fs,我目前正在构建一个用于文件备份的应用程序,它对文件系统进行了大量的读写操作。大部分都很好用,但我对应用程序的错误处理有点纠结 在下面的屏幕截图中,最后一个路径不是有效的目录,并返回一个异常,如您所见 我的问题是,是否可以用我自己的错误窗口替换标准错误窗口?或者在某些情况下忽略错误窗口的弹出窗口?第一次使用电子,如果这是一个明显的问题,那么很抱歉 谢谢 当您从readdir抛出错误时,它会被顶级uncaughtException处理程序捕获,由第一行“uncaughtException”指示 您

我目前正在构建一个用于文件备份的应用程序,它对文件系统进行了大量的读写操作。大部分都很好用,但我对应用程序的错误处理有点纠结

在下面的屏幕截图中,最后一个路径不是有效的目录,并返回一个异常,如您所见

我的问题是,是否可以用我自己的错误窗口替换标准错误窗口?或者在某些情况下忽略错误窗口的弹出窗口?第一次使用电子,如果这是一个明显的问题,那么很抱歉


谢谢

当您从
readdir
抛出错误时,它会被顶级
uncaughtException
处理程序捕获,由第一行“uncaughtException”指示

您需要做的是为主流程中的添加您自己的自定义处理程序,并从中显示您想要的任何对话框

看看这个模块

例如,您可以使用
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);
});

不幸的是,这对代码的某些部分不起作用,例如
require
语句。@m4heshd是否只是在所有顶级导入之前挂起
uncaughtException
事件的问题?(虽然看起来很奇怪)但它确实是出于某种原因发生的。自从Electron v3什么的,我几乎什么都试过了。只是放弃了。
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);
});