From c213971a2dc87554ce70b9019d04e93cd5311c0f Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Fri, 13 Jan 2017 12:28:11 -0800 Subject: [PATCH] Write a warning instead of crashing. --- lib/browser/rpc-server.js | 5 ++++- spec/api-ipc-spec.js | 6 +++--- spec/static/main.js | 14 ++++++++------ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index d6b9e8a61c3f..257c8ac575d9 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -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)) } } diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index 47f9b98bf52b..9f4ce0fb1ebc 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -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() diff --git a/spec/static/main.js b/spec/static/main.js index cb14f9d9aec5..a0ce1c24d380 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -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 })