fix(page): dispatch errors into page (#3550)

Errors thrown on the node side of the `page.exposeFunction` callback
should be dispatched into the page.

Fixes #3549
This commit is contained in:
Andrey Lushnikov 2018-11-15 14:51:34 -08:00 committed by GitHub
parent c185eeef61
commit 95a19c74bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 4 deletions

View File

@ -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;
};
@ -511,12 +511,48 @@ class Page extends EventEmitter {
*/
async _onBindingCalled(event) {
const {name, seq, args} = JSON.parse(event.payload);
let expression = null;
try {
const result = await this._pageBindings.get(name)(...args);
const expression = helper.evaluationString(deliverResult, name, seq, result);
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);
}
}

View File

@ -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() {