From c9ce921bb4e9a8c1a9c27833d53e8d0f134a5792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Urba=C5=84czyk?= Date: Fri, 18 Feb 2022 10:37:30 +0100 Subject: [PATCH] feat: throw errors, do not log them (#99) --- 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 86f0d72c..f2d1888a 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 62aaa456..10a902ac 100644 --- a/test/index.js +++ b/test/index.js @@ -6,18 +6,24 @@ const { serialize } = require('../lib/helpers'); 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', () => {