mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
parent
c185eeef61
commit
95a19c74bc
42
lib/Page.js
42
lib/Page.js
@ -428,7 +428,7 @@ class Page extends EventEmitter {
|
|||||||
}
|
}
|
||||||
const seq = (me['lastSeq'] || 0) + 1;
|
const seq = (me['lastSeq'] || 0) + 1;
|
||||||
me['lastSeq'] = seq;
|
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}));
|
binding(JSON.stringify({name: bindingName, seq, args}));
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
@ -511,12 +511,48 @@ class Page extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
async _onBindingCalled(event) {
|
async _onBindingCalled(event) {
|
||||||
const {name, seq, args} = JSON.parse(event.payload);
|
const {name, seq, args} = JSON.parse(event.payload);
|
||||||
|
let expression = null;
|
||||||
|
try {
|
||||||
const result = await this._pageBindings.get(name)(...args);
|
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);
|
this._client.send('Runtime.evaluate', { expression, contextId: event.executionContextId }).catch(debugError);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} name
|
||||||
|
* @param {number} seq
|
||||||
|
* @param {*} result
|
||||||
|
*/
|
||||||
function deliverResult(name, seq, 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);
|
window[name]['callbacks'].delete(seq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1033,6 +1033,33 @@ module.exports.addTests = function({testRunner, expect, headless}) {
|
|||||||
});
|
});
|
||||||
expect(result).toBe(36);
|
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}) => {
|
it('should be callable from-inside evaluateOnNewDocument', async({page, server}) => {
|
||||||
let called = false;
|
let called = false;
|
||||||
await page.exposeFunction('woof', function() {
|
await page.exposeFunction('woof', function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user