From aae73f5fd70fcd242ecf086a17db6802d54e3c59 Mon Sep 17 00:00:00 2001 From: Yaniv Efraim Date: Tue, 26 Jun 2018 01:10:36 +0300 Subject: [PATCH] feat(worker): implement pageerror event from a worker (#2795) Fixes #2761 --- lib/Page.js | 2 +- lib/Worker.js | 4 +++- test/worker.spec.js | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Page.js b/lib/Page.js index 5663097c..78ca2961 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -102,7 +102,7 @@ class Page extends EventEmitter { return; } const session = client._createSession(event.targetInfo.type, event.sessionId); - const worker = new Worker(session, event.targetInfo.url, this._addConsoleMessage.bind(this)); + const worker = new Worker(session, event.targetInfo.url, this._addConsoleMessage.bind(this), this._handleException.bind(this)); this._workers.set(event.sessionId, worker); this.emit(Page.Events.WorkerCreated, worker); diff --git a/lib/Worker.js b/lib/Worker.js index e2db0e58..db6a7271 100644 --- a/lib/Worker.js +++ b/lib/Worker.js @@ -22,8 +22,9 @@ class Worker extends EventEmitter { * @param {Puppeteer.CDPSession} client * @param {string} url * @param {function(!string, !Array)} consoleAPICalled + * @param {function(!Protocol.Runtime.ExceptionDetails)} exceptionThrown */ - constructor(client, url, consoleAPICalled) { + constructor(client, url, consoleAPICalled, exceptionThrown) { super(); this._client = client; this._url = url; @@ -39,6 +40,7 @@ class Worker extends EventEmitter { this._client.send('Runtime.enable', {}).catch(debugError); this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory))); + this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails)); } /** diff --git a/test/worker.spec.js b/test/worker.spec.js index 9d221734..4138cf4f 100644 --- a/test/worker.spec.js +++ b/test/worker.spec.js @@ -48,5 +48,11 @@ module.exports.addTests = function({testRunner, expect}) { const worker = await workerCreatedPromise; expect(await (await worker.executionContext()).evaluate('1+1')).toBe(2); }); + it('should report errors', async function({page}) { + const errorPromise = new Promise(x => page.on('pageerror', x)); + await page.evaluate(() => new Worker(`data:text/javascript, throw new Error('this is my error');`)); + const errorLog = await errorPromise; + expect(errorLog.message).toContain('this is my error'); + }); }); }; \ No newline at end of file