From aba61de9057820a391425bf1666210ff8478206c Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 21 Jul 2017 11:57:25 -0700 Subject: [PATCH] Make helper.getExceptionMessage synchronous We now have description of an exception, no need for a roundtrip to the backend. --- lib/FrameManager.js | 12 ++++-------- lib/Page.js | 4 ++-- lib/helper.js | 19 ++++--------------- test/test.js | 2 +- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/lib/FrameManager.js b/lib/FrameManager.js index 868a5e5d..50a62f8a 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -165,10 +165,8 @@ class FrameManager extends EventEmitter { } expression = `Promise.resolve(${expression})`; let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true }); - if (exceptionDetails) { - let message = await helper.getExceptionMessage(this._client, exceptionDetails); - throw new Error('Evaluation failed: ' + message); - } + if (exceptionDetails) + throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails)); return await helper.serializeRemoteObject(this._client, remoteObject); } @@ -190,10 +188,8 @@ class FrameManager extends EventEmitter { awaitPromise: true, returnByValue: false, }); - if (exceptionDetails) { - let message = await helper.getExceptionMessage(this._client, exceptionDetails); - throw new Error('Evaluation failed: ' + message); - } + if (exceptionDetails) + throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails)); /** * @param {string} selector diff --git a/lib/Page.js b/lib/Page.js index 71ce46fd..9f107932 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -207,8 +207,8 @@ class Page extends EventEmitter { /** * @param {!Object} exceptionDetails */ - async _handleException(exceptionDetails) { - let message = await helper.getExceptionMessage(this._client, exceptionDetails); + _handleException(exceptionDetails) { + let message = helper.getExceptionMessage(exceptionDetails); this.emit(Page.Events.PageError, new Error(message)); } diff --git a/lib/helper.js b/lib/helper.js index 7b8de515..94c53e16 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -25,24 +25,13 @@ class Helper { } /** - * @param {!Connection} client * @param {!Object} exceptionDetails * @return {string} */ - static async getExceptionMessage(client, exceptionDetails) { - let message = ''; - let exception = exceptionDetails.exception; - if (exception) { - let response = await client.send('Runtime.callFunctionOn', { - objectId: exception.objectId, - functionDeclaration: 'function() { return this.message; }', - returnByValue: true, - }); - message = response.result.value; - } else { - message = exceptionDetails.text; - } - + static getExceptionMessage(exceptionDetails) { + if (exceptionDetails.exception) + return exceptionDetails.exception.description; + let message = exceptionDetails.text; if (exceptionDetails.stackTrace) { for (let callframe of exceptionDetails.stackTrace.callFrames) { let location = callframe.url + ':' + callframe.lineNumber + ':' + callframe.columnNumber; diff --git a/test/test.js b/test/test.js index 811a75d2..bd751826 100644 --- a/test/test.js +++ b/test/test.js @@ -240,7 +240,7 @@ describe('Puppeteer', function() { await page.waitFor('*'); fail('Failed waitFor did not throw.'); } catch (e) { - expect(e.message).toContain('Evaluation failed: document.querySelector is not a function'); + expect(e.message).toContain('document.querySelector is not a function'); } })); it('should throw when frame is detached', SX(async function() {