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 () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.replyWithError(404)
nock('https://api.github.com').get('/users/nodisplayname').replyWithError(404)
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 () => {
const username = 'thisusernamedoesntexist'
nock('https://api.github.com')
.get(`/users/${username}`)
.reply(404, {
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.`,
)
})
@ -100,22 +96,19 @@ 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.)",
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, {
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',
@ -161,9 +154,7 @@ 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, {
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',
@ -175,9 +166,7 @@ test('fill in the name when an empty string is returned', async () => {
})
test('append http when no absolute link is provided', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
.reply(200, {
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',

View file

@ -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)