电子ipcmain如何优雅地处理错误

问题描述 投票:0回答:2
在电子中,如果我在后端的任何地方都丢下错误,它将进入自定义窗口。试图找到一种方法来抓住它,以将其推向我的应用程序中的自定义区域,我发现我可以通过

process.on('uncaughtException')

检测该过程。但是,我坚持尝试运行发件人以发送错误或报告。我尝试的是:

ipcMain.on('main', async (e, data) => { try { await someModule(data) process.on('uncaughtException', err => e.sender.send('error', err.message)) return e.sender.send('audit', 'No issues found') } catch (err) { console.log(err) } })
Module.js:

module.export = data => { throw Error('this is a test') }
在上面,我发送的两个都会将两个错误audit`转到渲染器。我已经研究了一种将“无名感”传递给三元的方法,但我找不到有关如何条件“ uncaughtexception”的任何文档,但我确实尝试过:

and

以上仅在存在错误时起作用,研究:

捕获节点jsapp
  • 的所有不发育感 nodejs untured例外处理
  • nonode.jsuntured例外 - 传递更多细节
  • 在电子中,如何拦截错误将其传递给渲染器,而不会抛出默认错误窗口?

如果您使用

electron uncaught-exception ipcmain
2个回答
5
投票

您将能够在渲染器过程中处理错误 ipcMain.handle

update:

这种方法的一个问题是,向渲染器过程发出的错误已被序列化,即使它用try/catch进行了处理,也会打印出它。
要解决此问题,您也可以处理主过程中的错误
// Main process ipcMain.handle('my-invokable-ipc', async (event, data) => { await someModule(data) return 'No issues found' }) // Renderer process async () => { try { const result = await ipcRenderer.invoke('my-invokable-ipc', data) console.log(result) // 'No issues found' if someModule did not throw an error } catch (err) { // you can handle someModule errors here } }

在解决方法时,您可以将错误代码扔向渲染器:

// Main process ipcMain.handle('my-invokable-ipc', async (event, data) => { try { await someModule(data) return 'No issues found' } catch (err) { // handle someModule errors and notify renderer process // return err.message or any other way you see fit } }) // Renderer process async () => { const result = await ipcRenderer.invoke('my-invokable-ipc', data) console.log(result) // 'No issues found' if someModule did not throw an error }
    

0
投票
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.