Skip to content

Commit

Permalink
fix: add common error handling for missing or wrong username (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
erdahuja authored and Berkmann18 committed Mar 17, 2019
1 parent 949d9d2 commit 3e046b7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/contributors/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ function getQuestions(options, username, contributions) {
options.repoType,
)} username?`,
when: !username,
validate: function validate(input) {
if (!input) {
return 'Username not provided'
}
return true
},
},
{
type: 'checkbox',
Expand Down Expand Up @@ -88,7 +94,6 @@ function getValidUserContributions(options, contributions) {
`${invalidUserContributions.toString()} is/are invalid contribution type(s)`,
)
}

return validUserContributions
}

Expand Down
24 changes: 24 additions & 0 deletions src/repo/__tests__/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ test('handle errors', async () => {
await rejects(getUserInfo('nodisplayname'))
})

test('Throw error when no username is provided', () => {
expect(getUserInfo).toThrow(
'No login when adding a contributor. Please specify a username.',
)
})

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',
})
try {
await getUserInfo(username)
} catch (error) {
expect(error.message).toEqual(
`Login not found when adding a contributor for username - ${username}.`,
)
}
})

test('handle github errors', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
Expand Down
11 changes: 10 additions & 1 deletion src/repo/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
hostname = 'https://github.com'
}

if (!username) {
throw new Error(
`No login when adding a contributor. Please specify a username.`,
)
}

const root = hostname.replace(/:\/\//, '://api.')
return request
.get({
Expand All @@ -68,11 +74,14 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
})
.then(res => {
const body = JSON.parse(res.body)

let profile = body.blog || body.html_url

// Github throwing specific errors as 200...
if (!profile && body.message) {
throw new Error(body.message)
throw new Error(
`Login not found when adding a contributor for username - ${username}.`,
)
}

profile = profile.startsWith('http') ? profile : `http://${profile}`
Expand Down

0 comments on commit 3e046b7

Please sign in to comment.