Skip to content

Commit

Permalink
fix(page): dispatch errors into page (#3550)
Browse files Browse the repository at this point in the history
Errors thrown on the node side of the `page.exposeFunction` callback
should be dispatched into the page.

Fixes #3549
  • Loading branch information
aslushnikov committed Nov 15, 2018
1 parent c185eee commit 95a19c7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
44 changes: 40 additions & 4 deletions lib/Page.js
Expand Up @@ -428,7 +428,7 @@ class Page extends EventEmitter {
}
const seq = (me['lastSeq'] || 0) + 1;
me['lastSeq'] = seq;
const promise = new Promise(fulfill => callbacks.set(seq, fulfill));
const promise = new Promise((resolve, reject) => callbacks.set(seq, {resolve, reject}));
binding(JSON.stringify({name: bindingName, seq, args}));
return promise;
};
Expand Down Expand Up @@ -511,12 +511,48 @@ class Page extends EventEmitter {
*/
async _onBindingCalled(event) {
const {name, seq, args} = JSON.parse(event.payload);
const result = await this._pageBindings.get(name)(...args);
const expression = helper.evaluationString(deliverResult, name, seq, result);
let expression = null;
try {
const result = await this._pageBindings.get(name)(...args);
expression = helper.evaluationString(deliverResult, name, seq, result);
} catch (error) {
if (error instanceof Error)
expression = helper.evaluationString(deliverError, name, seq, error.message, error.stack);
else
expression = helper.evaluationString(deliverErrorValue, name, seq, error);
}
this._client.send('Runtime.evaluate', { expression, contextId: event.executionContextId }).catch(debugError);

/**
* @param {string} name
* @param {number} seq
* @param {*} result
*/
function deliverResult(name, seq, result) {
window[name]['callbacks'].get(seq)(result);
window[name]['callbacks'].get(seq).resolve(result);
window[name]['callbacks'].delete(seq);
}

/**
* @param {string} name
* @param {number} seq
* @param {string} message
* @param {string} stack
*/
function deliverError(name, seq, message, stack) {
const error = new Error(message);
error.stack = stack;
window[name]['callbacks'].get(seq).reject(error);
window[name]['callbacks'].delete(seq);
}

/**
* @param {string} name
* @param {number} seq
* @param {*} value
*/
function deliverErrorValue(name, seq, value) {
window[name]['callbacks'].get(seq).reject(value);
window[name]['callbacks'].delete(seq);
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/page.spec.js
Expand Up @@ -1033,6 +1033,33 @@ module.exports.addTests = function({testRunner, expect, headless}) {
});
expect(result).toBe(36);
});
it('should throw exception in page context', async({page, server}) => {
await page.exposeFunction('woof', function() {
throw new Error('WOOF WOOF');
});
const {message, stack} = await page.evaluate(async() => {
try {
await woof();
} catch (e) {
return {message: e.message, stack: e.stack};
}
});
expect(message).toBe('WOOF WOOF');
expect(stack).toContain(__filename);
});
it('should support throwing "null"', async({page, server}) => {
await page.exposeFunction('woof', function() {
throw null;
});
const thrown = await page.evaluate(async() => {
try {
await woof();
} catch (e) {
return e;
}
});
expect(thrown).toBe(null);
});
it('should be callable from-inside evaluateOnNewDocument', async({page, server}) => {
let called = false;
await page.exposeFunction('woof', function() {
Expand Down

0 comments on commit 95a19c7

Please sign in to comment.