mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
parent
ff2c3bbca9
commit
2066da9ec7
23
lib/Page.js
23
lib/Page.js
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
10
test/test.js
10
test/test.js
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user