Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass on errors thrown in executeJavaScript #11158

Merged
merged 5 commits into from Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/browser/api/web-contents.js
Expand Up @@ -112,6 +112,16 @@ const webFrameMethods = [
]
const webFrameMethodsWithResult = []

const errorConstructors = {
Error,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError
}

const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
return new Promise((resolve, reject) => {
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
Expand All @@ -120,7 +130,14 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
if (typeof callback === 'function') callback(result)
resolve(result)
} else {
reject(error)
if (error.__ELECTRON_SERIALIZED_ERROR__ && errorConstructors[error.name]) {
const rehydratedError = new errorConstructors[error.name](error.message)
rehydratedError.stack = error.stack

reject(rehydratedError)
} else {
reject(error)
}
}
})
})
Expand Down
11 changes: 11 additions & 0 deletions lib/renderer/init.js
Expand Up @@ -45,6 +45,17 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (ev
event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, null, resolvedResult)
})
.catch((resolvedError) => {
if (resolvedError instanceof Error) {
// Errors get lost, because: JSON.stringify(new Error('Message')) === {}
// Take the serializable properties and construct a generic object
resolvedError = {
message: resolvedError.message,
stack: resolvedError.stack,
name: resolvedError.name,
__ELECTRON_SERIALIZED_ERROR__: true
}
}

event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, resolvedError)
})
}
Expand Down
20 changes: 20 additions & 0 deletions spec/api-browser-window-spec.js
Expand Up @@ -2514,6 +2514,15 @@ describe('BrowserWindow module', () => {
const code = `(() => "${expected}")()`
const asyncCode = `(() => new Promise(r => setTimeout(() => r("${expected}"), 500)))()`
const badAsyncCode = `(() => new Promise((r, e) => setTimeout(() => e("${expectedErrorMsg}"), 500)))()`
const errorTypes = new Set([
Error,
ReferenceError,
EvalError,
RangeError,
SyntaxError,
TypeError,
URIError
])

it('doesnt throw when no calback is provided', () => {
const result = ipcRenderer.sendSync('executeJavaScript', code, false)
Expand Down Expand Up @@ -2561,6 +2570,17 @@ describe('BrowserWindow module', () => {
done()
})
})
it('rejects the returned promise with an error if an Error.prototype is thrown', async () => {
for (const error in errorTypes) {
await new Promise((resolve) => {
ipcRenderer.send('executeJavaScript', `Promise.reject(new ${error.name}("Wamp-wamp")`, true)
ipcRenderer.once('executeJavaScript-promise-error-name', (event, name) => {
assert.equal(name, error.name)
resolve()
})
})
}
})
it('works after page load and during subframe load', (done) => {
w.webContents.once('did-finish-load', () => {
// initiate a sub-frame load, then try and execute script during it
Expand Down
4 changes: 4 additions & 0 deletions spec/static/main.js
Expand Up @@ -217,6 +217,10 @@ app.on('ready', function () {
window.webContents.send('executeJavaScript-promise-response', result)
}).catch((error) => {
window.webContents.send('executeJavaScript-promise-error', error)

if (error && error.name) {
window.webContents.send('executeJavaScript-promise-error-name', error.name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this only for tests? Can the 'executeJavaScript-promise-error' message be used instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly not (but this is a wonderful example of how un-intuitive this is) - if we send the received error over the IPC again, we'll get {} again 😆

}
})

if (!hasCallback) {
Expand Down