From 0574156c3c541967f6602b642033a936f57380e2 Mon Sep 17 00:00:00 2001 From: Raisel Melian Date: Wed, 6 Feb 2019 20:49:32 -0500 Subject: [PATCH 1/2] change parser to throw errors to callers and increment generator version --- lib/parser.js | 12 ++++++------ package.json | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index c62dad55e..a2ad9af1e 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -59,12 +59,12 @@ async function parse (filePath) { content = JSON.stringify(filePath); } else { console.error(`Could not find a valid asyncapi definition: ${filePath}`); - return; + throw `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 { @@ -72,7 +72,7 @@ async function parse (filePath) { } catch (e) { console.error('Can not parse the content of the AsyncAPI specification file'); console.error(e); - return; + throw e; } try { @@ -80,7 +80,7 @@ async function parse (filePath) { } 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 { @@ -88,7 +88,7 @@ async function parse (filePath) { } 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 { @@ -97,7 +97,7 @@ 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)); diff --git a/package.json b/package.json index f529b59e6..1105136be 100644 --- a/package.json +++ b/package.json @@ -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": { From b434b9e220201bb91da06e92569489d87af9337d Mon Sep 17 00:00:00 2001 From: Raisel Melian Date: Thu, 7 Feb 2019 12:54:55 -0500 Subject: [PATCH 2/2] better error handling --- lib/parser.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index a2ad9af1e..f21cf0067 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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'); @@ -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;