Skip to content

Commit

Permalink
fix: make error message more clear to locate which package is invalid
Browse files Browse the repository at this point in the history
Credit: @gemwuu #63
  • Loading branch information
天玎 authored and wraithgar committed Feb 10, 2022
1 parent c6a9e12 commit 8cb4527
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/npa.js
Expand Up @@ -85,14 +85,16 @@ function resolve (name, spec, where, arg) {
}
}

function invalidPackageName (name, valid) {
const err = new Error(`Invalid package name "${name}": ${valid.errors.join('; ')}`)
function invalidPackageName (name, valid, raw) {
// eslint-disable-next-line max-len
const err = new Error(`Invalid package name "${name}" of package "${raw}": ${valid.errors.join('; ')}.`)
err.code = 'EINVALIDPACKAGENAME'
return err
}
function invalidTagName (name) {

function invalidTagName (name, raw) {
// eslint-disable-next-line max-len
const err = new Error(`Invalid tag name "${name}": Tags may not have any characters that encodeURIComponent encodes.`)
const err = new Error(`Invalid tag name "${name}" of package "${raw}": Tags may not have any characters that encodeURIComponent encodes.`)
err.code = 'EINVALIDTAGNAME'
return err
}
Expand Down Expand Up @@ -124,7 +126,7 @@ function Result (opts) {
Result.prototype.setName = function (name) {
const valid = validatePackageName(name)
if (!valid.validForOldPackages) {
throw invalidPackageName(name, valid)
throw invalidPackageName(name, valid, this.raw)
}

this.name = name
Expand Down Expand Up @@ -356,9 +358,8 @@ function fromRegistry (res) {
res.type = 'range'
} else {
if (encodeURIComponent(spec) !== spec) {
throw invalidTagName(spec)
throw invalidTagName(spec, res.raw)
}

res.type = 'tag'
}
return res
Expand Down
14 changes: 14 additions & 0 deletions test/basic.js
Expand Up @@ -693,3 +693,17 @@ t.test('strict 8909 compliance mode', t => {

t.end()
})

t.test('error message', t => {
t.throws(() => npa('lodash.has@>=^4'), {
// eslint-disable-next-line max-len
message: 'Invalid tag name ">=^4" of package "lodash.has@>=^4": Tags may not have any characters that encodeURIComponent encodes.',
})

t.throws(() => npa('lodash.has @^4'), {
// eslint-disable-next-line max-len
message: 'Invalid package name "lodash.has " of package "lodash.has @^4": name cannot contain leading or trailing spaces; name can only contain URL-friendly characters.',
})

t.end()
})

0 comments on commit 8cb4527

Please sign in to comment.