Skip to content

Commit 1e2e239

Browse files
authored
Merge pull request #34 from asyncapi/change-parser-to-rethrow-errors
change parser to throw errors to callers and increment generator version
2 parents 945f3e0 + b434b9e commit 1e2e239

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/parser.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,36 @@ async function parse (filePath) {
5959
content = JSON.stringify(filePath);
6060
} else {
6161
console.error(`Could not find a valid asyncapi definition: ${filePath}`);
62-
return;
62+
throw new Error(`Could not find a valid asyncapi definition: ${filePath}`);
6363
}
6464
} catch (e) {
6565
console.error('Can not load the content of the AsyncAPI specification file');
6666
console.error(e);
67-
return;
67+
throw e;
6868
}
6969

7070
try {
7171
parsedContent = parseContent(content);
7272
} catch (e) {
7373
console.error('Can not parse the content of the AsyncAPI specification file');
7474
console.error(e);
75-
return;
75+
throw e;
7676
}
7777

7878
try {
7979
dereferencedJSON = await dereference(parsedContent);
8080
} catch (e) {
8181
console.error('Can not dereference the JSON obtained from the content of the AsyncAPI specification file');
8282
console.error(e);
83-
return;
83+
throw e;
8484
}
8585

8686
try {
8787
bundledJSON = await bundle(dereferencedJSON);
8888
} catch (e) {
8989
console.error('Can not bundle the JSON obtained from the content of the AsyncAPI specification file');
9090
console.error(e);
91-
return;
91+
throw e;
9292
}
9393

9494
try {
@@ -97,24 +97,32 @@ async function parse (filePath) {
9797
} catch (e) {
9898
console.error('Invalid JSON obtained from the content of the AsyncAPI specification file');
9999
console.error(e);
100-
return;
100+
throw e;
101101
}
102102

103103
return JSON.parse(JSON.stringify(parsed));
104104
};
105105

106106
async function parseText (content) {
107-
const parsedContent = parseContent(content);
108-
if (typeof parsedContent !== 'object' || parsedContent === null) {
109-
throw new Error('Invalid YAML content.');
110-
}
111-
const dereferencedJSON = await dereference(parsedContent);
112-
const bundledJSON = await bundle(dereferencedJSON);
113-
const asyncAPIschema = require('asyncapi')[bundledJSON.asyncapi];
107+
let parsedStringified;
114108

115-
const parsed = await validate(bundledJSON, asyncAPIschema);
109+
try {
110+
const parsedContent = parseContent(content);
111+
if (typeof parsedContent !== 'object' || parsedContent === null) {
112+
throw new Error('Invalid YAML content.');
113+
}
114+
const dereferencedJSON = await dereference(parsedContent);
115+
const bundledJSON = await bundle(dereferencedJSON);
116+
const asyncAPIschema = require('asyncapi')[bundledJSON.asyncapi];
116117

117-
return JSON.parse(JSON.stringify(parsed));
118+
const parsed = await validate(bundledJSON, asyncAPIschema);
119+
120+
parsedStringified = JSON.parse(JSON.stringify(parsed));
121+
} catch (e) {
122+
throw e;
123+
}
124+
125+
return parsedStringified
118126
};
119127

120128
module.exports = parse;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "asyncapi-generator",
3-
"version": "0.6.0",
3+
"version": "0.6.1",
44
"description": "The AsyncAPI generator. It can generate documentation, code, anything!",
55
"main": "./lib/generator.js",
66
"bin": {

0 commit comments

Comments
 (0)