Skip to content

Commit

Permalink
Fail gracefully when chromium failed to download (puppeteer#430)
Browse files Browse the repository at this point in the history
Fail gracefully when chromium failed to download

This patch changes both install.js and Launcher.js to inform how
chromium could be downloaded manually.
  • Loading branch information
aslushnikov authored Aug 21, 2017
1 parent 598f439 commit c1731dd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
16 changes: 13 additions & 3 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@
*/

const Downloader = require('./utils/ChromiumDownloader');
const platform = Downloader.currentPlatform();
const revision = require('./package').puppeteer.chromium_revision;
const ProgressBar = require('progress');

const revisionInfo = Downloader.revisionInfo(platform, revision);
// Do nothing if the revision is already downloaded.
if (Downloader.revisionInfo(Downloader.currentPlatform(), revision))
if (revisionInfo.downloaded)
return;

let allRevisions = Downloader.downloadedRevisions();
Downloader.downloadRevision(Downloader.currentPlatform(), revision, onProgress)
Downloader.downloadRevision(platform, revision, onProgress)
// Remove previous chromium revisions.
.then(() => Promise.all(allRevisions.map(({platform, revision}) => Downloader.removeRevision(platform, revision))))
.catch(error => console.error('Download failed: ' + error.message));
.catch(onError);

function onError(error) {
console.error(`ERROR: Failed to download chromium r${revision}!
- Download chromium manually:
${revisionInfo.url}
- Extract chromium into ${revisionInfo.folderPath}
* Chromium executable should be at ${revisionInfo.executablePath}`);
}

let progressBar = null;
function onProgress(bytesTotal, delta) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Launcher {
if (typeof chromeExecutable !== 'string') {
let chromiumRevision = require('../package.json').puppeteer.chromium_revision;
let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
console.assert(revisionInfo.downloaded, `Chromium revision is not downloaded. Run "npm install"`);
chromeExecutable = revisionInfo.executablePath;
}
if (Array.isArray(options.args))
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ else
const Downloader = require('../utils/ChromiumDownloader');
const chromiumRevision = require('../package.json').puppeteer.chromium_revision;
const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
console.assert(revisionInfo, `Chromium r${chromiumRevision} is not downloaded. Run 'npm install' and try to re-run tests.`);
console.assert(revisionInfo.downloaded, `Chromium r${chromiumRevision} is not downloaded. Run 'npm install' and try to re-run tests.`);
}

let server;
Expand Down
9 changes: 5 additions & 4 deletions utils/ChromiumDownloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,11 @@ module.exports = {
/**
* @param {string} platform
* @param {string} revision
* @return {?{executablePath: string}}
* @return {!{folderPath: string, executablePath: string, downloaded: boolean, url: string}}
*/
revisionInfo: function(platform, revision) {
console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`);
let folderPath = getFolderPath(platform, revision);
if (!fs.existsSync(folderPath))
return null;
let executablePath = '';
if (platform === 'mac')
executablePath = path.join(folderPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium');
Expand All @@ -146,7 +144,10 @@ module.exports = {
else
throw 'Unsupported platfrom: ' + platfrom;
return {
executablePath: executablePath
executablePath,
folderPath,
downloaded: fs.existsSync(folderPath),
url: util.format(downloadURLs[platform], revision)
};
},
};
Expand Down

0 comments on commit c1731dd

Please sign in to comment.