feat: nicer error message for page.addScriptTag (#1754)

This commit is contained in:
Joel Einbinder 2020-04-12 18:46:53 -07:00 committed by GitHub
parent 8536fa885e
commit a7572c7f09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 8 deletions

View File

@ -135,13 +135,13 @@ export class FFExecutionContext implements js.ExecutionContextDelegate {
}
}
function checkException(exceptionDetails?: any) {
if (exceptionDetails) {
if (exceptionDetails.value)
throw new Error('Evaluation failed: ' + JSON.stringify(exceptionDetails.value));
else
throw new Error('Evaluation failed: ' + exceptionDetails.text + '\n' + exceptionDetails.stack);
}
function checkException(exceptionDetails?: Protocol.Runtime.ExceptionDetails) {
if (!exceptionDetails)
return;
if (exceptionDetails.value)
throw new Error('Evaluation failed: ' + JSON.stringify(exceptionDetails.value));
else
throw new Error('Evaluation failed: ' + exceptionDetails.text + '\n' + exceptionDetails.stack);
}
export function deserializeValue({unserializableValue, value}: Protocol.Runtime.RemoteObject) {

View File

@ -580,7 +580,7 @@ export class Frame {
script.type = options.type;
const promise = new Promise((res, rej) => {
script.onload = res;
script.onerror = rej;
script.onerror = e => rej(typeof e === 'string' ? new Error(e) : new Error(`Failed to load script at ${script.src}`));
});
document.head.appendChild(script);
await promise;

View File

@ -654,6 +654,12 @@ describe('Page.addScriptTag', function() {
await page.addScriptTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedfile.js' }).catch(e => error = e);
expect(error).toBeTruthy();
});
it('should throw a nice error when the request fails', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
const url = server.PREFIX + '/this_does_not_exist.js';
const error = await page.addScriptTag({url}).catch(e => e);
expect(error.message).toContain(url);
});
});
describe('Page.addStyleTag', function() {