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
如果您使用
您将能够在渲染器过程中处理错误
ipcMain.handle
要解决此问题,您也可以处理主过程中的错误
// 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
}