Skip to content

Commit

Permalink
improved error handling eduardoboucas#328
Browse files Browse the repository at this point in the history
  • Loading branch information
travisdowns committed Feb 3, 2020
1 parent 67fc513 commit 2dd7bb0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/GitService.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class GitService {
errorData.data = err.message
}

return errorHandler('PARSING_ERROR', errorData)
throw errorHandler('PARSING_ERROR', errorData)
}
}

Expand Down
9 changes: 8 additions & 1 deletion test/helpers/sampleData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=="
Expand Down Expand Up @@ -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:
Expand Down
39 changes: 31 additions & 8 deletions test/unit/lib/GitHub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,41 @@ 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)

let exception = 'thrown'
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()
Expand Down Expand Up @@ -290,25 +317,21 @@ 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,
options.commitTitle
)
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)
Expand Down

0 comments on commit 2dd7bb0

Please sign in to comment.