Skip to content

Commit

Permalink
feat: throw errors, do not log them (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Feb 18, 2022
1 parent c292539 commit c9ce921
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit c9ce921

Please sign in to comment.