From 44b39f388ac5f96c909e61079024e4d5c8e06df5 Mon Sep 17 00:00:00 2001 From: David Kettler <21echoes@gmail.com> Date: Fri, 9 Feb 2018 11:35:48 -0800 Subject: [PATCH] For private packages not hosted on NPM, requesting data about them from NPM will fail. Wrap these places in try/catch, or guard against invalid access for bulk fetches. --- src/cmd.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/cmd.js b/src/cmd.js index 482dcdb..70a8591 100755 --- a/src/cmd.js +++ b/src/cmd.js @@ -213,7 +213,12 @@ async function fetchPkg (client, pkgName) { staleOk: true, auth: registryAuthToken() } - return client.getAsync(url, opts) + try { + return await client.getAsync(url, opts) + } catch (err) { + console.error(chalk`\n{red error fetching}`, pkgName, err) + return {} + } } function printTable (authorsSeeking, pkgNamesSeeking, authorsPkgNames, directPkgNames) { @@ -281,14 +286,20 @@ async function bulkFetchPkgDownloads (pkgNames) { const url = DOWNLOADS_URL + pkgNamesSubset.join(',') const res = await got(url, { json: true }) Object.keys(res.body).forEach(pkgName => { - pkgDownloads[pkgName] = res.body[pkgName].downloads + if (res.body[pkgName]) { + pkgDownloads[pkgName] = res.body[pkgName].downloads + } }) } await Promise.all(scopedPkgNames.map(async scopedPkgName => { const url = DOWNLOADS_URL + scopedPkgName - const res = await got(url, { json: true }) - pkgDownloads[scopedPkgName] = res.body.downloads + try { + const res = await got(url, { json: true }) + pkgDownloads[scopedPkgName] = res.body.downloads + } catch (err) { + console.error(chalk`\n{red error fetching}`, scopedPkgName, err) + } })) return pkgDownloads