Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Raisel Melian committed Feb 7, 2019
1 parent 0574156 commit b434b9e
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function parse (filePath) {
content = JSON.stringify(filePath);
} else {
console.error(`Could not find a valid asyncapi definition: ${filePath}`);
throw `Could not find a valid asyncapi definition: ${filePath}`;
throw new Error(`Could not find a valid asyncapi definition: ${filePath}`);
}
} catch (e) {
console.error('Can not load the content of the AsyncAPI specification file');
Expand Down Expand Up @@ -104,17 +104,25 @@ async function parse (filePath) {
};

async function parseText (content) {
const parsedContent = parseContent(content);
if (typeof parsedContent !== 'object' || parsedContent === null) {
throw new Error('Invalid YAML content.');
}
const dereferencedJSON = await dereference(parsedContent);
const bundledJSON = await bundle(dereferencedJSON);
const asyncAPIschema = require('asyncapi')[bundledJSON.asyncapi];
let parsedStringified;

const parsed = await validate(bundledJSON, asyncAPIschema);
try {
const parsedContent = parseContent(content);
if (typeof parsedContent !== 'object' || parsedContent === null) {
throw new Error('Invalid YAML content.');
}
const dereferencedJSON = await dereference(parsedContent);
const bundledJSON = await bundle(dereferencedJSON);
const asyncAPIschema = require('asyncapi')[bundledJSON.asyncapi];

return JSON.parse(JSON.stringify(parsed));
const parsed = await validate(bundledJSON, asyncAPIschema);

parsedStringified = JSON.parse(JSON.stringify(parsed));
} catch (e) {
throw e;
}

return parsedStringified
};

module.exports = parse;
Expand Down

0 comments on commit b434b9e

Please sign in to comment.