fix: add common error handling for missing or wrong username (#171)

This commit is contained in:
Deepak 2019-03-17 20:00:07 +05:30 committed by Maximilian Berkmann
parent 949d9d2012
commit 3e046b740c
3 changed files with 40 additions and 2 deletions

View file

@ -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',
@ -88,7 +94,6 @@ function getValidUserContributions(options, contributions) {
`${invalidUserContributions.toString()} is/are invalid contribution type(s)`,
)
}
return validUserContributions
}

View file

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

View file

@ -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({
@ -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}`