From cc2288ef31ba76f51feb846a7b831a96f37148de Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@keylocation.sg> Date: Wed, 18 Oct 2017 08:25:42 +0200 Subject: [PATCH] fix: add try-catch to getFileList (#975) --- lib/api/github.js | 29 ++++++++++++++++++----------- lib/api/gitlab.js | 21 +++++++++++++-------- test/api/github.spec.js | 10 ++++++++++ test/api/gitlab.spec.js | 8 ++++++++ 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/lib/api/github.js b/lib/api/github.js index e0ca936639..5f901a568d 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -233,19 +233,26 @@ async function getFileList(branchName) { if (config.fileList) { return config.fileList; } - const res = await get( - `repos/${config.repoName}/git/trees/${branchName}?recursive=true` - ); - if (res.body.truncated) { - logger.warn( - { repository: config.repoName }, - 'repository tree is truncated' + try { + const res = await get( + `repos/${config.repoName}/git/trees/${branchName}?recursive=true` ); + if (res.body.truncated) { + logger.warn( + { repository: config.repoName }, + 'repository tree is truncated' + ); + } + config.fileList = res.body.tree + .filter(item => item.type === 'blob') + .map(item => item.path) + .sort(); + } catch (err) { + // TODO: change this from warn to info once we know exactly why it happens + logger.warn({ repository: config.repoName }, 'Error retrieving git tree'); + config.fileList = []; } - config.fileList = res.body.tree - .filter(item => item.type === 'blob') - .map(item => item.path) - .sort(); + return config.fileList; } diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js index 55ac109ad2..d22716f406 100644 --- a/lib/api/gitlab.js +++ b/lib/api/gitlab.js @@ -128,14 +128,19 @@ async function getFileList(branchName) { if (config.fileList) { return config.fileList; } - const res = await get( - `projects/${config.repoName}/repository/tree?ref=${branchName}&recursive=true&per_page=100`, - { paginate: true } - ); - config.fileList = res.body - .filter(item => item.type === 'blob') - .map(item => item.path) - .sort(); + try { + const res = await get( + `projects/${config.repoName}/repository/tree?ref=${branchName}&recursive=true&per_page=100`, + { paginate: true } + ); + config.fileList = res.body + .filter(item => item.type === 'blob') + .map(item => item.path) + .sort(); + } catch (err) { + logger.warn('Error retrieving git tree'); + config.fileList = []; + } return config.fileList; } diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 6d13a27aae..92e48fc18a 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -469,6 +469,16 @@ describe('api/github', () => { expect(get.mock.calls).toMatchSnapshot(); }); }); + describe('getFileList', () => { + it('returns empty array if error', async () => { + await initRepo('some/repo', 'token'); + get.mockImplementationOnce(() => { + throw new Error('some error'); + }); + const files = await github.findFilePaths('someething'); + expect(files).toEqual([]); + }); + }); describe('findFilePaths(fileName)', () => { it('warns if truncated result', async () => { await initRepo('some/repo', 'token'); diff --git a/test/api/gitlab.spec.js b/test/api/gitlab.spec.js index e8eb9e2aad..4ff05f92dc 100644 --- a/test/api/gitlab.spec.js +++ b/test/api/gitlab.spec.js @@ -191,6 +191,14 @@ describe('api/gitlab', () => { }); }); describe('findFilePaths(fileName)', () => { + it('returns empty array if error', async () => { + await initRepo('some/repo', 'token'); + get.mockImplementationOnce(() => { + throw new Error('some error'); + }); + const files = await gitlab.findFilePaths('someething'); + expect(files).toEqual([]); + }); it('warns if truncated result', async () => { await initRepo('some/repo', 'token'); get.mockImplementationOnce(() => ({ -- GitLab