fix(github): show the actual error message (#286)

* fix: 🐛 show actual github error message

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

Co-authored-by: Berkmann18 <maxieberkmann@gmail.com>
This commit is contained in:
Shai Reznik 2020-09-20 13:52:21 +03:00 committed by GitHub
parent 1affe41308
commit 5c506a526c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 47 deletions

View file

@ -58,9 +58,7 @@ async function rejects(promise) {
} }
test('handle errors', async () => { test('handle errors', async () => {
nock('https://api.github.com') nock('https://api.github.com').get('/users/nodisplayname').replyWithError(404)
.get('/users/nodisplayname')
.replyWithError(404)
await rejects(getUserInfo('nodisplayname')) await rejects(getUserInfo('nodisplayname'))
}) })
@ -73,15 +71,13 @@ test('Throw error when no username is provided', () => {
test('Throw error when non existent username is provided', async () => { test('Throw error when non existent username is provided', async () => {
const username = 'thisusernamedoesntexist' const username = 'thisusernamedoesntexist'
nock('https://api.github.com') nock('https://api.github.com').get(`/users/${username}`).reply(404, {
.get(`/users/${username}`) message: 'Not Found',
.reply(404, { documentation_url:
message: 'Not Found', 'https://developer.github.com/v3/users/#get-a-single-user',
documentation_url: })
'https://developer.github.com/v3/users/#get-a-single-user',
})
await expect(getUserInfo(username)).rejects.toThrow( await expect(getUserInfo(username)).rejects.toThrow(
`Login not found when adding a contributor for username - ${username}.`, `The username ${username} doesn't exist on GitHub.`,
) )
}) })
@ -100,27 +96,24 @@ test('Throw error when missing enterprise authentication', async () => {
) )
}) })
test('handle github errors', async () => { test('handle API rate github errors', async () => {
nock('https://api.github.com') const githubErrorMessage =
.get('/users/nodisplayname') "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."
.reply(200, { nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
message: message: 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.)", documentation_url: 'https://developer.github.com/v3/#rate-limiting',
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 () => { test('fill in the name when null is returned', async () => {
nock('https://api.github.com') nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
.get('/users/nodisplayname') login: 'nodisplayname',
.reply(200, { name: null,
login: 'nodisplayname', avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
name: null, html_url: 'https://github.com/nodisplayname',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400', })
html_url: 'https://github.com/nodisplayname',
})
const info = await getUserInfo('nodisplayname') const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('nodisplayname') expect(info.name).toBe('nodisplayname')
@ -161,28 +154,24 @@ test('attaches no token when not supplied', async () => {
}) })
test('fill in the name when an empty string is returned', async () => { test('fill in the name when an empty string is returned', async () => {
nock('https://api.github.com') nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
.get('/users/nodisplayname') login: 'nodisplayname',
.reply(200, { name: '',
login: 'nodisplayname', avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
name: '', html_url: 'https://github.com/nodisplayname',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400', })
html_url: 'https://github.com/nodisplayname',
})
const info = await getUserInfo('nodisplayname') const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('nodisplayname') expect(info.name).toBe('nodisplayname')
}) })
test('append http when no absolute link is provided', async () => { test('append http when no absolute link is provided', async () => {
nock('https://api.github.com') nock('https://api.github.com').get('/users/nodisplayname').reply(200, {
.get('/users/nodisplayname') login: 'nodisplayname',
.reply(200, { name: '',
login: 'nodisplayname', avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400',
name: '', html_url: 'www.github.com/nodisplayname',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400', })
html_url: 'www.github.com/nodisplayname',
})
const info = await getUserInfo('nodisplayname') const info = await getUserInfo('nodisplayname')
expect(info.profile).toBe('http://www.github.com/nodisplayname') expect(info.profile).toBe('http://www.github.com/nodisplayname')

View file

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