Skip to content

Commit

Permalink
Merge pull request #34 from asyncapi/change-parser-to-rethrow-errors
Browse files Browse the repository at this point in the history
change parser to throw errors to callers and increment generator version
  • Loading branch information
rmelian authored Feb 8, 2019
2 parents 945f3e0 + b434b9e commit 1e2e239
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,36 @@ async function parse (filePath) {
content = JSON.stringify(filePath);
} else {
console.error(`Could not find a valid asyncapi definition: ${filePath}`);
return;
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');
console.error(e);
return;
throw e;
}

try {
parsedContent = parseContent(content);
} catch (e) {
console.error('Can not parse the content of the AsyncAPI specification file');
console.error(e);
return;
throw e;
}

try {
dereferencedJSON = await dereference(parsedContent);
} catch (e) {
console.error('Can not dereference the JSON obtained from the content of the AsyncAPI specification file');
console.error(e);
return;
throw e;
}

try {
bundledJSON = await bundle(dereferencedJSON);
} catch (e) {
console.error('Can not bundle the JSON obtained from the content of the AsyncAPI specification file');
console.error(e);
return;
throw e;
}

try {
Expand All @@ -97,24 +97,32 @@ async function parse (filePath) {
} catch (e) {
console.error('Invalid JSON obtained from the content of the AsyncAPI specification file');
console.error(e);
return;
throw e;
}

return JSON.parse(JSON.stringify(parsed));
};

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asyncapi-generator",
"version": "0.6.0",
"version": "0.6.1",
"description": "The AsyncAPI generator. It can generate documentation, code, anything!",
"main": "./lib/generator.js",
"bin": {
Expand Down

0 comments on commit 1e2e239

Please sign in to comment.