diff --git a/lib/GitHub.js b/lib/GitHub.js index cd4a01cc..d51295ca 100644 --- a/lib/GitHub.js +++ b/lib/GitHub.js @@ -182,7 +182,7 @@ class GitHub extends GitService { try { return await super.readFile(filePath, getFullResponse) } catch (err) { - return errorHandler('GITHUB_READING_FILE', {err}) + throw errorHandler('GITHUB_READING_FILE', {err}) } } diff --git a/lib/GitService.js b/lib/GitService.js index 2add1c12..6aa0cd18 100644 --- a/lib/GitService.js +++ b/lib/GitService.js @@ -85,7 +85,7 @@ class GitService { errorData.data = err.message } - return errorHandler('PARSING_ERROR', errorData) + throw errorHandler('PARSING_ERROR', errorData) } } diff --git a/test/helpers/sampleData.js b/test/helpers/sampleData.js index 35aab01f..b8e87e56 100644 --- a/test/helpers/sampleData.js +++ b/test/helpers/sampleData.js @@ -75,7 +75,7 @@ comments: clientId: "L4M3LIshioHbe3j+vMxEbGlCGDhyIcQF2jhmVOUp8DqC+RqNgvZSQp7qYYmjPPoyjFCVOsu5aHwcD1FkMlEaxLTqYOYUeq49Wb6uxePTBycmW14JI6fiM/PYTm6nqKH5fB/7wnohVgK+/1IVAF6DA7UAs0Ju+srlnqEbn30f84sySOeR+V6t9aF7OiF9DsGedsTfVrfj8opptwQe7nycsxQaTxvmwgQgP9FrDYH+PGy/3ThpQsPj+/Mnvbnn7PMJEJlZFtGZsMWWcE2anJlJ7fbHKNPNNg6l2qosh6/kMTrloCU6wA67ouai0OFiNR+gyQaqUiL3NMgN4k39nZuwOg==" clientSecret: "0anSY5FBW+YF8BuFRRYxKX0yjDGU8/HhdirQMh+xFOEIvBQ8n/PdYi3qv2p4ngpFo5gb3PY2W6oWvHYLGgbFFse3YvzP5cbKRG0BN90hanlpVwmtAsapC7UepvOOUmNCRKHJ/pYICYcleUX/xGRiTugl9rcVw1MLg7kxcCuEMWcsBc9qCA9YHRcN3ucP+rT9x/2hLMiUmv3glYYZNgyQ3x1iqpOuTAIMeIkxMQo83vxGR57fpAx4+Yn1+hALhSl3sGaesUZhY9Py/OEZDFVWiN9RvrM4ND0IcfqyaQ4DuRPu2g8Es5fbmgSQoqkNPOcHT2+40pDbz3FPgz6QJou+pQ==" redirectUri: "https://my-test-site.com" - + gitlabAuth: clientId: "Rr9d1XmVVMe8ogldH6rBtdGhf49c29ldwcBVsiMn6DLRAiYWmHY08eKC6xLnP6mXwMe/qCHJ6JMKURDODL8Yjm+nQf09zynkIRCr4J7tRHh4bPAYXPG+W1+TK7l8QD4gC+WXamxJiggwGCaDtNylI1QQhbKtevv7n/T+Iq98rBj8SLxxpi3qR0oZeN/zsoQsDYgzZ+HgvA3hY+5H897ijx1oBjoTsfI1Sfx8Qqix/QLZoXorOUJyEo+83WWvTEgo8X3OyFbXGZ758Kw6A7fcHxu8oVAjDvtFJFiwrDb4iBz9rffx7llZXjkcjzYzfwFcjG7mzZnfYgn9WcCwO7zlsA==" clientSecret: "1zzjCrOZQ9dVs1p/WLgT8Lvwez3EKd1tp3D+7P5uGlEdqP1RN7kQvcaqOmOpm5SIY6g+yKJQGZq9G/IqUoKdsZDhA2VGYGXVzETU6eB48AL0OXlFumhjzJoGAXpnqDWzfevglkVuAkivBv6o9S1r/FL1GydwlRwWcYU6NNJjjkB04A00B4s0J7FRR3VFRxpJqDznHgXgT32E2+F3s6enh9/aErqi9uqn+iVtw7gvbd9PN1ejlo95R3BVNKUxNi2Dn4BbsH3MjQG4DyuzX8BiS9Nb+Xt+CwLygTT/i4C5Aj+KkMjAEiYOyttFbk3jkvYVXJ1XtW+taloBVPYCHgDzmg==" @@ -150,6 +150,13 @@ module.exports.config3 = `comments: siteKey: "123456789" secret: "@reCaptchaSecret@"` +module.exports.configInvalid = `invalid: + - x + y + foo +bar +` + module.exports.prBody1 = `Dear human, Here's a new entry for your approval. :tada: diff --git a/test/unit/lib/GitHub.test.js b/test/unit/lib/GitHub.test.js index eb7d51ef..7612c42d 100644 --- a/test/unit/lib/GitHub.test.js +++ b/test/unit/lib/GitHub.test.js @@ -111,7 +111,7 @@ describe('GitHub interface', () => { expect(scope.isDone()).toBe(true) }) - test('returns an error if parsing fails for the given file', async () => { + test('returns an error if the config file cannot be read', async () => { const filePath = 'path/to/file.yml' const githubInstance = await new GitHub(req.params) @@ -119,6 +119,33 @@ describe('GitHub interface', () => { try { await githubInstance.readFile(filePath) exception = 'not thrown' + } catch (err) { + expect(err._smErrorCode).toEqual('GITHUB_READING_FILE') + expect(err.message).toBeDefined() + } + + expect(exception).toBe('thrown') + }) + + test('returns an error if the config file cannot be parsed', async () => { + const filePath = 'path/to/file.yml' + const githubInstance = await new GitHub(req.params) + + const scope = nock((/api\.github\.com/), { + reqheaders: { + authorization: 'token '.concat('1q2w3e4r') + } + }) + .get('/repos/johndoe/foobar/contents/path/to/file.yml?ref=master') + .reply(200, { + content: btoa(sampleData.configInvalid) + }) + + let exception = 'thrown' + try { + let content = await githubInstance.readFile(filePath) + console.log('content %o', content) + exception = 'not thrown' } catch (err) { expect(err._smErrorCode).toEqual('PARSING_ERROR') expect(err.message).toBeDefined() @@ -290,15 +317,13 @@ describe('GitHub interface', () => { } }) .put('/repos/johndoe/foobar/contents/path/to/file.txt') - .reply(200, { - number: 123 - }) + .replyWithError('{ message: "an error" }') const githubInstance = await new GitHub(req.params) let exception = 'thrown' try { - await githubInstance.writeFile( + let ret = await githubInstance.writeFile( options.path, options.content, options.branch, @@ -306,9 +331,7 @@ describe('GitHub interface', () => { ) exception = 'not thrown' } catch (err) { - expect(err).toEqual({ - _smErrorCode: 'GITHUB_WRITING_FILE' - }) + expect(err._smErrorCode).toEqual('GITHUB_WRITING_FILE') } expect(exception).toBe('thrown') expect(scope.isDone()).toBe(true)