【问题标题】:custom error window/handling in ElectronElectron 中的自定义错误窗口/处理
【发布时间】:2018-11-14 02:37:39
【问题描述】:

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

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

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 很抱歉,如果这是一个明显的问题。

谢谢!

【问题讨论】:

    标签: javascript node.js electron fs


    【解决方案1】:

    当您从readdir 抛出错误时,它会被顶级uncaughtException 处理程序捕获,由第一行表示:“未捕获的异常”。

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

    看看dialog 模块。

    例如,您可以使用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 或其他东西以来,我几乎尝试了所有方法。就放弃了。
    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 2019-10-19
    • 2018-10-12
    • 1970-01-01
    • 2013-02-12
    • 2011-01-29
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多