@@ -6,6 +6,15 @@ const { safeRe: re, t } = require('../internal/re')
6
6
7
7
const parseOptions = require ( '../internal/parse-options' )
8
8
const { compareIdentifiers } = require ( '../internal/identifiers' )
9
+
10
+ function handleErrorOnSemver ( semver , errorMessage , noThrow ) {
11
+ if ( ! noThrow ) {
12
+ throw new TypeError ( errorMessage )
13
+ } else {
14
+ semver . errorMessage = errorMessage
15
+ }
16
+ }
17
+
9
18
class SemVer {
10
19
constructor ( version , options ) {
11
20
options = parseOptions ( options )
@@ -18,13 +27,11 @@ class SemVer {
18
27
version = version . version
19
28
}
20
29
} else if ( typeof version !== 'string' ) {
21
- throw new TypeError ( `Invalid version. Must be a string. Got type "${ typeof version } ".` )
30
+ return handleErrorOnSemver ( this , `Invalid version. Must be a string. Got type "${ typeof version } ".` , options . noThrow )
22
31
}
23
32
24
33
if ( version . length > MAX_LENGTH ) {
25
- throw new TypeError (
26
- `version is longer than ${ MAX_LENGTH } characters`
27
- )
34
+ return handleErrorOnSemver ( this , `version is longer than ${ MAX_LENGTH } characters` , options . noThrow )
28
35
}
29
36
30
37
debug ( 'SemVer' , version , options )
@@ -37,7 +44,7 @@ class SemVer {
37
44
const m = version . trim ( ) . match ( options . loose ? re [ t . LOOSE ] : re [ t . FULL ] )
38
45
39
46
if ( ! m ) {
40
- throw new TypeError ( `Invalid Version: ${ version } ` )
47
+ return handleErrorOnSemver ( this , `Invalid Version: ${ version } ` , options . noThrow )
41
48
}
42
49
43
50
this . raw = version
@@ -48,15 +55,15 @@ class SemVer {
48
55
this . patch = + m [ 3 ]
49
56
50
57
if ( this . major > MAX_SAFE_INTEGER || this . major < 0 ) {
51
- throw new TypeError ( 'Invalid major version' )
58
+ return handleErrorOnSemver ( this , 'Invalid major version' , options . noThrow )
52
59
}
53
60
54
61
if ( this . minor > MAX_SAFE_INTEGER || this . minor < 0 ) {
55
- throw new TypeError ( 'Invalid minor version' )
62
+ return handleErrorOnSemver ( this , 'Invalid minor version' , options . noThrow )
56
63
}
57
64
58
65
if ( this . patch > MAX_SAFE_INTEGER || this . patch < 0 ) {
59
- throw new TypeError ( 'Invalid patch version' )
66
+ return handleErrorOnSemver ( this , 'Invalid patch version' , options . noThrow )
60
67
}
61
68
62
69
// numberify any prerelease numeric ids
0 commit comments