Skip to content

Commit e219bb4

Browse files
authored
fix: throw on bad version with correct error message (#552)
1 parent 503a4e5 commit e219bb4

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

classes/semver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SemVer {
1616
version = version.version
1717
}
1818
} else if (typeof version !== 'string') {
19-
throw new TypeError(`Invalid Version: ${version}`)
19+
throw new TypeError(`Invalid Version: ${require('util').inspect(version)}`)
2020
}
2121

2222
if (version.length > MAX_LENGTH) {

functions/diff.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const parse = require('./parse')
1+
const parse = require('./parse.js')
22

33
const diff = (version1, version2) => {
4-
const v1 = parse(version1)
5-
const v2 = parse(version2)
4+
const v1 = parse(version1, null, true)
5+
const v2 = parse(version2, null, true)
66
const comparison = v1.compare(v2)
77

88
if (comparison === 0) {

functions/parse.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
const { MAX_LENGTH } = require('../internal/constants')
21
const SemVer = require('../classes/semver')
3-
const parse = (version, options) => {
2+
const parse = (version, options, throwErrors = false) => {
43
if (version instanceof SemVer) {
54
return version
65
}
7-
8-
if (typeof version !== 'string') {
9-
return null
10-
}
11-
12-
if (version.length > MAX_LENGTH) {
13-
return null
14-
}
15-
166
try {
177
return new SemVer(version, options)
188
} catch (er) {
19-
return null
9+
if (!throwErrors) {
10+
return null
11+
}
12+
throw er
2013
}
2114
}
2215

test/functions/diff.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ test('diff versions test', (t) => {
4343

4444
t.end()
4545
})
46+
47+
test('throws on bad version', (t) => {
48+
t.throws(() => {
49+
diff('bad', '1.2.3')
50+
}, {
51+
message: 'Invalid Version: bad',
52+
name: 'TypeError',
53+
})
54+
t.end()
55+
})

test/functions/parse.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ t.test('returns null instead of throwing when presented with garbage', t => {
99
t.equal(parse(v, opts), null, msg))
1010
})
1111

12+
t.test('throw errors if asked to', t => {
13+
t.throws(() => {
14+
parse('bad', null, true)
15+
}, {
16+
name: 'TypeError',
17+
message: 'Invalid Version: bad',
18+
})
19+
t.throws(() => {
20+
parse([], null, true)
21+
}, {
22+
name: 'TypeError',
23+
message: 'Invalid Version: []',
24+
})
25+
t.end()
26+
})
27+
1228
t.test('parse a version into a SemVer object', t => {
1329
t.match(parse('1.2.3'), new SemVer('1.2.3'))
1430
const s = new SemVer('4.5.6')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy