From e219bb454036a0c23e34407591f921c8edb688e7 Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 17 Apr 2023 10:00:55 -0700 Subject: [PATCH] fix: throw on bad version with correct error message (#552) --- classes/semver.js | 2 +- functions/diff.js | 6 +++--- functions/parse.js | 17 +++++------------ test/functions/diff.js | 10 ++++++++++ test/functions/parse.js | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/classes/semver.js b/classes/semver.js index ea62fa0d..25ee889d 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -16,7 +16,7 @@ class SemVer { version = version.version } } else if (typeof version !== 'string') { - throw new TypeError(`Invalid Version: ${version}`) + throw new TypeError(`Invalid Version: ${require('util').inspect(version)}`) } if (version.length > MAX_LENGTH) { diff --git a/functions/diff.js b/functions/diff.js index d15b1def..fafc11c4 100644 --- a/functions/diff.js +++ b/functions/diff.js @@ -1,8 +1,8 @@ -const parse = require('./parse') +const parse = require('./parse.js') const diff = (version1, version2) => { - const v1 = parse(version1) - const v2 = parse(version2) + const v1 = parse(version1, null, true) + const v2 = parse(version2, null, true) const comparison = v1.compare(v2) if (comparison === 0) { diff --git a/functions/parse.js b/functions/parse.js index 42ac5ddb..459b3b17 100644 --- a/functions/parse.js +++ b/functions/parse.js @@ -1,22 +1,15 @@ -const { MAX_LENGTH } = require('../internal/constants') const SemVer = require('../classes/semver') -const parse = (version, options) => { +const parse = (version, options, throwErrors = false) => { if (version instanceof SemVer) { return version } - - if (typeof version !== 'string') { - return null - } - - if (version.length > MAX_LENGTH) { - return null - } - try { return new SemVer(version, options) } catch (er) { - return null + if (!throwErrors) { + return null + } + throw er } } diff --git a/test/functions/diff.js b/test/functions/diff.js index aa35c3d0..74a80569 100644 --- a/test/functions/diff.js +++ b/test/functions/diff.js @@ -43,3 +43,13 @@ test('diff versions test', (t) => { t.end() }) + +test('throws on bad version', (t) => { + t.throws(() => { + diff('bad', '1.2.3') + }, { + message: 'Invalid Version: bad', + name: 'TypeError', + }) + t.end() +}) diff --git a/test/functions/parse.js b/test/functions/parse.js index 16183dc0..f3ca4cd8 100644 --- a/test/functions/parse.js +++ b/test/functions/parse.js @@ -9,6 +9,22 @@ t.test('returns null instead of throwing when presented with garbage', t => { t.equal(parse(v, opts), null, msg)) }) +t.test('throw errors if asked to', t => { + t.throws(() => { + parse('bad', null, true) + }, { + name: 'TypeError', + message: 'Invalid Version: bad', + }) + t.throws(() => { + parse([], null, true) + }, { + name: 'TypeError', + message: 'Invalid Version: []', + }) + t.end() +}) + t.test('parse a version into a SemVer object', t => { t.match(parse('1.2.3'), new SemVer('1.2.3')) const s = new SemVer('4.5.6')