From 521adfce3432ef019811420eb8a879813186c389 Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Thu, 17 Feb 2022 11:21:14 +0100 Subject: [PATCH] feat: throw errors, do not log them --- lib/index.js | 9 +++------ test/index.js | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index f9a55b94..83cb6418 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,16 +34,13 @@ lib.convert = (asyncapi, version, options = {}) => { const toVersion = conversionVersions.indexOf(version); if (fromVersion === -1 || toVersion === -1) { - console.error(`Cannot convert from ${parsed.asyncapi} to ${version}.`); - return; + throw new Error(`Cannot convert from ${parsed.asyncapi} to ${version}.`); } if (fromVersion > toVersion) { - console.error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`); - return; + throw new Error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`); } if (fromVersion === toVersion) { - console.error(`Cannot convert to the same version.`); - return; + throw new Error(`Cannot convert to the same version.`); } // add 1 to `fromVersion` because we convert from previous to next diff --git a/test/index.js b/test/index.js index 62e9902c..1c768ddd 100644 --- a/test/index.js +++ b/test/index.js @@ -5,18 +5,24 @@ const { convert } = require('../lib'); describe('#convert', () => { it('should not convert to lowest version', () => { - const result = convert(`asyncapi: '2.1.0'`, '2.0.0'); - assert.strictEqual(result, undefined); + assert.throws( + () => convert(`asyncapi: '2.1.0'`, '2.0.0'), + /^Error: Cannot downgrade from 2.1.0 to 2.0.0.$/ + ); }); it('should not convert from non existing version', () => { - const result = convert(`asyncapi: '2.0.0-rc3'`, '2.1.0'); - assert.strictEqual(result, undefined); + assert.throws( + () => convert(`asyncapi: '2.0.0-rc3'`, '2.1.0'), + /^Error: Cannot convert from 2.0.0-rc3 to 2.1.0.$/ + ); }); it('should not convert to this same version', () => { - const result = convert(`asyncapi: '2.1.0'`, '2.1.0'); - assert.strictEqual(result, undefined); + assert.throws( + () => convert(`asyncapi: '2.1.0'`, '2.1.0'), + /^Error: Cannot convert to the same version.$/ + ); }); it('should convert from 1.0.0 to 2.0.0-rc1', () => {