gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:31:33 25 4
gpt4 key购买 nike

我目前正在构建一个文件备份应用程序,它对文件系统进行了相当多的读写操作。大部分功能都很好,但我在应用程序的错误处理方面遇到了一些困难。

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

enter image description here

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

谢谢!

最佳答案

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

您需要做的是为 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);
});

关于javascript - Electron 中的自定义错误窗口/处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com