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) { function checkException(exceptionDetails?: Protocol.Runtime.ExceptionDetails) {
if (exceptionDetails) { if (!exceptionDetails)
if (exceptionDetails.value) return;
throw new Error('Evaluation failed: ' + JSON.stringify(exceptionDetails.value)); if (exceptionDetails.value)
else throw new Error('Evaluation failed: ' + JSON.stringify(exceptionDetails.value));
throw new Error('Evaluation failed: ' + exceptionDetails.text + '\n' + exceptionDetails.stack); else
} throw new Error('Evaluation failed: ' + exceptionDetails.text + '\n' + exceptionDetails.stack);
} }
export function deserializeValue({unserializableValue, value}: Protocol.Runtime.RemoteObject) { export function deserializeValue({unserializableValue, value}: Protocol.Runtime.RemoteObject) {

View file

@ -580,7 +580,7 @@ export class Frame {
script.type = options.type; script.type = options.type;
const promise = new Promise((res, rej) => { const promise = new Promise((res, rej) => {
script.onload = res; 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); document.head.appendChild(script);
await promise; 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); await page.addScriptTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedfile.js' }).catch(e => error = e);
expect(error).toBeTruthy(); 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() { describe('Page.addStyleTag', function() {