From f09bd7e5250396e695d246653b26aa39ba6f8fdc Mon Sep 17 00:00:00 2001 From: Raisel Melian Date: Fri, 25 Jan 2019 11:17:12 -0500 Subject: [PATCH] #2 catch possible errors from json conversion --- convert.js | 9 +++++++-- lib/converter.js | 2 +- test/samples/asyncapi.json | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/convert.js b/convert.js index e2c2b0d..02474e1 100644 --- a/convert.js +++ b/convert.js @@ -16,8 +16,13 @@ const command = async (specFilePath, targetFormat) => { process.exit(1); } - const document = await converter.convert(specFilePath, targetFormat); - console.log(document); + try { + const document = await converter.convert(specFilePath, targetFormat); + console.log(document); + }catch (e) { + console.error(e); + } + }; diff --git a/lib/converter.js b/lib/converter.js index ff5f55b..55a70cc 100644 --- a/lib/converter.js +++ b/lib/converter.js @@ -2,13 +2,13 @@ const parser = require('./parser'); const YAML = require('js-yaml'); - const formats = Object.freeze({ JSON: "json", YAML: "yaml" }); function convertToJson(content) { + let jsonObject = JSON.parse(content); return YAML.safeDump(jsonObject); diff --git a/test/samples/asyncapi.json b/test/samples/asyncapi.json index 6354088..11884d8 100644 --- a/test/samples/asyncapi.json +++ b/test/samples/asyncapi.json @@ -1,5 +1,5 @@ { - "asyncapid": "1.2.0", + "asyncapi": "1.2.0", "info": { "title": "Streetlights API", "version": "1.0.0",