Throw errors instead of returning reason

Throw errors instead of returning reason and throw errors if the entry has no root files
This commit is contained in:
Luis Tejeda
2022-06-06 15:58:09 -05:00
parent 2e73ed22bb
commit 4ccfa86271
3 changed files with 93 additions and 22 deletions

View File

@@ -4921,26 +4921,23 @@ module.exports = {
try {
res.output.data = this.mapProcessRelatedFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data,
inputRelatedFiles.origin, version, inputRelatedFiles.bundleFormat, toBundle);
if (res.output.data === undefined || res.output.data.result === false ||
res.output.data.length === 0) {
res.result = false;
}
}
catch (error) {
if (error instanceof ParseError) {
return {
result: false,
reason: error.message
};
}
else {
throw (error);
}
}
if (res.output.data.result === false) {
res.result = res.output.data.result;
res.error = res.output.data.error;
let newError = new Error('There was an error during the process');
newError.stack = error.stack;
throw (newError);
}
return res;
}
else {
return res;
throw new Error('Input should have at least one root file');
}
},

View File

@@ -401,12 +401,14 @@ describe('bundle files method - 3.0', function () {
}
]
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.false;
expect(res.reason).to.equal('Invalid format. Input must be in YAML or JSON ' +
'format. Specification is not a valid YAML. YAMLException: duplicated mapping' +
' key at line 30, column -54:\n components:\n ^');
try {
await Converter.bundle(input);
}
catch (error) {
expect(error.message).to.equal('Invalid format. Input must be in YAML or JSON ' +
'format. Specification is not a valid YAML. YAMLException: duplicated mapping' +
' key at line 30, column -54:\n components:\n ^');
}
});
it('Should return bundled file from same_ref_different_source', async function () {
@@ -746,6 +748,76 @@ describe('bundle files method - 3.0', function () {
expect(res.result).to.be.true;
expect(res.output.data.bundledContent).to.be.equal(expected);
});
it('Should take the root file from data array', async function () {
let contentRootFile = fs.readFileSync(sameRefDiffSource + '/root.yaml', 'utf8'),
user = fs.readFileSync(sameRefDiffSource + '/schemas/user/user.yaml', 'utf8'),
client = fs.readFileSync(sameRefDiffSource + '/schemas/client/client.yaml', 'utf8'),
specialUser = fs.readFileSync(sameRefDiffSource + '/schemas/user/special.yaml', 'utf8'),
specialClient = fs.readFileSync(sameRefDiffSource + '/schemas/client/special.yaml', 'utf8'),
magic = fs.readFileSync(sameRefDiffSource + '/schemas/client/magic.yaml', 'utf8'),
expected = fs.readFileSync(sameRefDiffSource + '/expected.json', 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
data: [
{
path: '/root.yaml',
content: contentRootFile
},
{
path: '/schemas/user/user.yaml',
content: user
},
{
path: '/schemas/user/special.yaml',
content: specialUser
},
{
path: '/schemas/client/client.yaml',
content: client
},
{
path: '/schemas/client/special.yaml',
content: specialClient
},
{
path: '/schemas/client/magic.yaml',
content: magic
}
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data.bundledContent).to.be.equal(expected);
});
it('Should throw error when input has not root files', async function () {
let user = fs.readFileSync(schemaFromResponse + '/schemas/user.yaml', 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
rootFiles: [],
data: [
{
path: '/schemas/user.yaml',
content: user
}
],
options: {},
bundleFormat: 'JSON'
};
try {
await Converter.bundle(input);
}
catch (error) {
expect(error.message).to.equal('Input should have at least one root file');
}
});
});

View File

@@ -30,7 +30,7 @@ let expect = require('chai').expect,
describe('detectRelatedFiles method', function () {
it('should return empty data when there is no root in the entry', async function () {
it('should return error when there is no root in the entry', async function () {
let contentFile = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
input = {
type: 'folder',
@@ -44,10 +44,12 @@ describe('detectRelatedFiles method', function () {
}
]
};
const res = await Converter.detectRelatedFiles(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data.length).to.equal(0);
try {
await Converter.detectRelatedFiles(input);
}
catch (error) {
expect(error.message).to.equal('Input should have at least one root file');
}
});
it('should locate root and return empty data when there is no ref', async function () {