Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw errors, do not log them #99

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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