mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 21:46:29 +00:00
fix: add common error handling for missing or wrong username (#171)
This commit is contained in:
parent
949d9d2012
commit
3e046b740c
3 changed files with 40 additions and 2 deletions
|
@ -26,6 +26,12 @@ function getQuestions(options, username, contributions) {
|
||||||
options.repoType,
|
options.repoType,
|
||||||
)} username?`,
|
)} username?`,
|
||||||
when: !username,
|
when: !username,
|
||||||
|
validate: function validate(input) {
|
||||||
|
if (!input) {
|
||||||
|
return 'Username not provided'
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'checkbox',
|
type: 'checkbox',
|
||||||
|
@ -88,7 +94,6 @@ function getValidUserContributions(options, contributions) {
|
||||||
`${invalidUserContributions.toString()} is/are invalid contribution type(s)`,
|
`${invalidUserContributions.toString()} is/are invalid contribution type(s)`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return validUserContributions
|
return validUserContributions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,30 @@ test('handle errors', async () => {
|
||||||
await rejects(getUserInfo('nodisplayname'))
|
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 () => {
|
test('handle github errors', async () => {
|
||||||
nock('https://api.github.com')
|
nock('https://api.github.com')
|
||||||
.get('/users/nodisplayname')
|
.get('/users/nodisplayname')
|
||||||
|
|
|
@ -60,6 +60,12 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
|
||||||
hostname = 'https://github.com'
|
hostname = 'https://github.com'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!username) {
|
||||||
|
throw new Error(
|
||||||
|
`No login when adding a contributor. Please specify a username.`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const root = hostname.replace(/:\/\//, '://api.')
|
const root = hostname.replace(/:\/\//, '://api.')
|
||||||
return request
|
return request
|
||||||
.get({
|
.get({
|
||||||
|
@ -68,11 +74,14 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
const body = JSON.parse(res.body)
|
const body = JSON.parse(res.body)
|
||||||
|
|
||||||
let profile = body.blog || body.html_url
|
let profile = body.blog || body.html_url
|
||||||
|
|
||||||
// Github throwing specific errors as 200...
|
// Github throwing specific errors as 200...
|
||||||
if (!profile && body.message) {
|
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}`
|
profile = profile.startsWith('http') ? profile : `http://${profile}`
|
||||||
|
|
Loading…
Reference in a new issue