Skip to content

Commit

Permalink
fix(github): show the actual error message (#286)
Browse files Browse the repository at this point in the history
* fix: 🐛 show actual github error message

* refactor(github): replaced "github" with "GitHub"

Co-authored-by: Berkmann18 <maxieberkmann@gmail.com>
  • Loading branch information
shairez and Berkmann18 authored Sep 20, 2020
1 parent 1affe41 commit 5c506a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 47 deletions.
77 changes: 33 additions & 44 deletions src/repo/__tests__/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ async function rejects(promise) {
}

test('handle errors', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.replyWithError(404)
nock('https://api.github.com').get('/users/nodisplayname').replyWithError(404)

await rejects(getUserInfo('nodisplayname'))
})
Expand All @@ -73,15 +71,13 @@ test('Throw error when no username is provided', () => {

test('Throw error when non existent username is provided', async () => {
const username = 'thisusernamedoesntexist'
nock('https://api.github.com')
.get(`/users/${username}`)
.reply(404, {
message: 'Not Found',
documentation_url:
'https://developer.github.com/v3/users/#get-a-single-user',
})
nock('https://api.github.com').get(`/users/${username}`).reply(404, {
message: 'Not Found',
documentation_url:
'https://developer.github.com/v3/users/#get-a-single-user',
})
await expect(getUserInfo(username)).rejects.toThrow(
`Login not found when adding a contributor for username - ${username}.`,
`The username ${username} doesn't exist on GitHub.`,
)
})

Expand All @@ -100,27 +96,24 @@ test('Throw error when missing enterprise authentication', async () => {
)
})

test('handle github errors', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.reply(200, {
message:
"API rate limit exceeded for 0.0.0.0. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
documentation_url: 'https://developer.github.com/v3/#rate-limiting',
})
test('handle API rate github errors', async () => {
const githubErrorMessage =
"API rate limit exceeded for 0.0.0.0. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details."
nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
message: githubErrorMessage,
documentation_url: 'https://developer.github.com/v3/#rate-limiting',
})

await rejects(getUserInfo('nodisplayname'))
await expect(getUserInfo('nodisplayname')).rejects.toThrow(githubErrorMessage)
})

test('fill in the name when null is returned', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.reply(200, {
login: 'nodisplayname',
name: null,
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
html_url: 'https://github.com/nodisplayname',
})
nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
login: 'nodisplayname',
name: null,
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
html_url: 'https://github.com/nodisplayname',
})

const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('nodisplayname')
Expand Down Expand Up @@ -161,28 +154,24 @@ test('attaches no token when not supplied', async () => {
})

test('fill in the name when an empty string is returned', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.reply(200, {
login: 'nodisplayname',
name: '',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
html_url: 'https://github.com/nodisplayname',
})
nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
login: 'nodisplayname',
name: '',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
html_url: 'https://github.com/nodisplayname',
})

const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('nodisplayname')
})

test('append http when no absolute link is provided', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.reply(200, {
login: 'nodisplayname',
name: '',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
html_url: 'www.github.com/nodisplayname',
})
nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
login: 'nodisplayname',
name: '',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
html_url: 'www.github.com/nodisplayname',
})

const info = await getUserInfo('nodisplayname')
expect(info.profile).toBe('http://www.github.com/nodisplayname')
Expand Down
8 changes: 5 additions & 3 deletions src/repo/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ const getUserInfo = function (username, hostname, optionalPrivateToken) {

// Github throwing specific errors as 200...
if (!profile && body.message) {
throw new Error(
`Login not found when adding a contributor for username - ${username}.`,
)
if (body.message.toLowerCase().includes('api rate limit exceeded')) {
throw new Error(body.message)
} else {
throw new Error(`The username ${username} doesn't exist on GitHub.`)
}
}

profile = parseHttpUrl(profile)
Expand Down

0 comments on commit 5c506a5

Please sign in to comment.