Page.evaluate should reject in case of evaluation error

This patch makes Page.evaluate to throw an error if there
was an error in evaluated code.
This commit is contained in:
Andrey Lushnikov 2017-06-16 10:34:13 -07:00
parent ff2c3bbca9
commit 2066da9ec7
2 changed files with 31 additions and 2 deletions

View File

@ -330,9 +330,10 @@ class Page extends EventEmitter {
expression: code
});
if (response.exceptionDetails) {
this._handleException(response.exceptionDetails);
await throwException(this._client, response.exceptionDetails);
return;
}
var remoteObject = response.result;
if (remoteObject.type !== 'object')
return remoteObject.value;
@ -347,11 +348,29 @@ class Page extends EventEmitter {
objectId: remoteObject.objectId
});
if (response.exceptionDetails) {
this._handleException(response.exceptionDetails);
await throwException(this._client, response.exceptionDetails);
return;
}
return response.result.value;
/**
* @param {!Object} exceptionDetails
* @return {!Promise}
*/
async function throwException(client, exceptionDetails) {
var exception = exceptionDetails.exception;
if (!exception) {
throw new Error('Evaluation failed with ' + exceptionDetails.text);
return;
}
var response = await client.send('Runtime.callFunctionOn', {
objectId: exception.objectId,
functionDeclaration: 'function() { return this.toString(); }',
returnByValue: true,
});
throw new Error('Evaluation failed with ' + response.result.value);
}
}
/**

View File

@ -64,6 +64,16 @@ describe('Puppeteer', function() {
});
expect(result).toBe(27);
}));
it('should reject promise with exception', SX(async function() {
var error = null;
try {
await page.evaluate(() => not.existing.object.property);
} catch (e) {
error = e;
}
expect(error).toBeTruthy();
expect(error.message).toContain('ReferenceError');
}));
});
it('Page Events: ConsoleMessage', SX(async function() {