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}`)
.reply(404, {
message: 'Not Found', message: 'Not Found',
documentation_url: documentation_url:
'https://developer.github.com/v3/users/#get-a-single-user', '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,22 +96,19 @@ 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')
.reply(200, {
login: 'nodisplayname', login: 'nodisplayname',
name: null, name: null,
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400', 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 () => { 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')
.reply(200, {
login: 'nodisplayname', login: 'nodisplayname',
name: '', name: '',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400', 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 () => { 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')
.reply(200, {
login: 'nodisplayname', login: 'nodisplayname',
name: '', name: '',
avatar_url: 'https://avatars2.githubusercontent.com/u/3869412?v=3&s=400', 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... // 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)