Write a warning instead of crashing.

This commit is contained in:
Charlie Hess 2017-01-13 12:28:11 -08:00 committed by Kevin Sawicki
parent bc2f1e8199
commit c213971a2d
3 changed files with 15 additions and 10 deletions

View file

@ -160,6 +160,9 @@ const rendererMissingErrorMessage = (meta, args, callIntoRenderer) => {
if (remoteEvents.length > 0) {
message += `\nRemote event names: ${remoteEvents.join(', ')}`
remoteEvents.forEach((eventName) => {
sender.removeListener(eventName, callIntoRenderer)
})
}
return message
@ -215,7 +218,7 @@ const unwrapArgs = function (sender, args) {
if (!sender.isDestroyed() && webContentsId === sender.getId()) {
sender.send('ELECTRON_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args))
} else {
throw new Error(rendererMissingErrorMessage(meta, args, callIntoRenderer))
console.warn(rendererMissingErrorMessage(meta, args, callIntoRenderer))
}
}

View file

@ -495,7 +495,7 @@ describe('ipc module', function () {
assert.equal(w.listenerCount('test'), 0)
})
it('throws an error when a function is called in a destroyed renderer', (done) => {
it('shows a warning when a function is called in a destroyed renderer', (done) => {
w = new BrowserWindow({
show: false
})
@ -506,8 +506,8 @@ describe('ipc module', function () {
'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler'
].join('\n')
const errorMessage = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.equal(errorMessage, expectedMessage)
const warningMessage = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.equal(warningMessage, expectedMessage)
done()
})
w.webContents.reload()

View file

@ -251,11 +251,13 @@ ipcMain.on('prevent-next-new-window', (event, id) => {
})
ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => {
const contents = webContents.fromId(id)
try {
contents.emit(eventName, {sender: contents})
event.returnValue = null
} catch (error) {
event.returnValue = error.message
const consoleWarn = console.warn
let lastWarning = null
console.warn = (message) => {
lastWarning = message
}
const contents = webContents.fromId(id)
contents.emit(eventName, {sender: contents})
event.returnValue = lastWarning
console.warn = consoleWarn
})