From ab2974f0b69ad830b670a75e937f1333731bb461 Mon Sep 17 00:00:00 2001 From: Ivan Demchenko Date: Thu, 8 Mar 2018 12:36:22 +1100 Subject: [PATCH 1/4] fixed error messages of JSON parsing is failed --- __test__/index.spec.js | 28 ++++++++++++++++++++++++++++ package.json | 6 ++++++ src/helper.js | 33 +++++++++++++++++++++++++++++++++ src/index.js | 11 ++++++----- 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 __test__/index.spec.js create mode 100644 src/helper.js diff --git a/__test__/index.spec.js b/__test__/index.spec.js new file mode 100644 index 0000000..477aca5 --- /dev/null +++ b/__test__/index.spec.js @@ -0,0 +1,28 @@ +var ramlToSwagger = require('../src'); + +describe('converting', () => { + it('should fail fast if json is wrong', () => { + const testJson = '{"some": "json" "that-is": "wrong"}'; + const testData = { + title: 'test', + version: '2.0.0', + securitySchemes: [], + resources: [], + schemas: [ + { + TestSchema: testJson + } + ] + }; + + expect(() => { + ramlToSwagger.convert(testData); + }).toThrowError([ + 'Unexpected string in JSON at position 16', + [ + '{"some": "json" "that-is": "wrong"}', + '---------------^' + ].join('\n') + ].join('\n')); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index 48fdb9e..9274c74 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "1.1.0", "description": "Script for converting RAML format into Swagger 2.0", "main": "src/index.js", + "scripts": { + "test": "jest" + }, "repository": { "type": "git", "url": "git://github.com/APIs-guru/raml-to-swagger.git" @@ -27,5 +30,8 @@ "jsonpath": "^0.2.5", "lodash": "^4.1.0", "urijs": "^1.17.0" + }, + "devDependencies": { + "jest": "^22.4.2" } } diff --git a/src/helper.js b/src/helper.js new file mode 100644 index 0000000..2225a17 --- /dev/null +++ b/src/helper.js @@ -0,0 +1,33 @@ +function mkArray(lim) { + return Array.from(Array(lim), function(a, i) { return i; }); +} + +function extractErrorPositionFromErrorMsg(msg) { + const res = /Unexpected string in JSON at position (\d+)/.exec(msg); + return res ? parseInt(res[1], 10) : null; +} + +function extractErrorPlace(rawJson, position) { + const context = 20; + return rawJson.substring(position - context, position + context); +} + +function showFancySyntaxException(rawJson, e) { + var context = []; + var pos = extractErrorPositionFromErrorMsg(e.message); + if (pos) { + context.push(extractErrorPlace(rawJson, pos)); + context.push(mkArray(pos -1).map(function() { return '-'; }).join('') + '^'); + } + return [ + e.message, + context.join('\n') + ].join('\n'); +} + +module.exports = { + showFancySyntaxException: showFancySyntaxException, + mkArray: mkArray, + extractErrorPositionFromErrorMsg: extractErrorPositionFromErrorMsg, + extractErrorPlace: extractErrorPlace +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 39d66e9..61f1925 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ var _ = require('lodash'); var jsonCompat = require('json-schema-compatibility'); var HttpStatus = require('http-status-codes').getStatusText; var jp = require('jsonpath'); +var H = require('./helper'); exports.convert = function (raml) { //FIXME: @@ -344,17 +345,17 @@ function parseBody(ramlBody, srMethod) { }); } -function convertSchema(schema) { - if (_.isUndefined(schema)) +function convertSchema(rawSchema) { + if (_.isUndefined(rawSchema)) return; - assert(_.isString(schema)); + assert(_.isString(rawSchema)); try { - var schema = JSON.parse(schema); + var schema = JSON.parse(rawSchema); } catch (e) { - return undefined; + throw new Error(H.showFancySyntaxException(rawSchema, e)); } delete schema.id; From 19d9c533cec4e24603177a71dffc9265593d4f4d Mon Sep 17 00:00:00 2001 From: Ivan Demchenko Date: Fri, 9 Mar 2018 11:53:02 +1100 Subject: [PATCH 2/4] wrap JSON.parse itself --- src/helper.js | 12 +++++++++++- src/index.js | 7 +------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/helper.js b/src/helper.js index 2225a17..ec5ab10 100644 --- a/src/helper.js +++ b/src/helper.js @@ -25,9 +25,19 @@ function showFancySyntaxException(rawJson, e) { ].join('\n'); } +function safeJsonParse(raw) { + try { + return JSON.parse(raw); + } + catch (e) { + throw new Error(H.showFancySyntaxException(raw, e)); + } +} + module.exports = { showFancySyntaxException: showFancySyntaxException, mkArray: mkArray, extractErrorPositionFromErrorMsg: extractErrorPositionFromErrorMsg, - extractErrorPlace: extractErrorPlace + extractErrorPlace: extractErrorPlace, + safeJsonParse: safeJsonParse } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 61f1925..0b19037 100644 --- a/src/index.js +++ b/src/index.js @@ -351,12 +351,7 @@ function convertSchema(rawSchema) { assert(_.isString(rawSchema)); - try { - var schema = JSON.parse(rawSchema); - } - catch (e) { - throw new Error(H.showFancySyntaxException(rawSchema, e)); - } + var schema = H.safeJsonParse(rawSchema); delete schema.id; delete schema.$schema; From 773fb3e7e3db1f54603fa0e8b97dc7da12f6fe73 Mon Sep 17 00:00:00 2001 From: Ivan Demchenko Date: Fri, 23 Mar 2018 11:31:50 +1100 Subject: [PATCH 3/4] cleaned up the code and used the library for error messages --- __test__/index.spec.js | 6 ++---- package.json | 1 + src/helper.js | 43 ------------------------------------------ src/index.js | 9 +++++++-- 4 files changed, 10 insertions(+), 49 deletions(-) delete mode 100644 src/helper.js diff --git a/__test__/index.spec.js b/__test__/index.spec.js index 477aca5..bb5f976 100644 --- a/__test__/index.spec.js +++ b/__test__/index.spec.js @@ -19,10 +19,8 @@ describe('converting', () => { ramlToSwagger.convert(testData); }).toThrowError([ 'Unexpected string in JSON at position 16', - [ - '{"some": "json" "that-is": "wrong"}', - '---------------^' - ].join('\n') + '1: {"some": "json" "that-is": "wrong"}', + '------------------^' ].join('\n')); }); }); \ No newline at end of file diff --git a/package.json b/package.json index 9274c74..9512cf4 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "urijs": "^1.17.0" }, "devDependencies": { + "better-error-message-for-json-parse": "^0.1.6", "jest": "^22.4.2" } } diff --git a/src/helper.js b/src/helper.js deleted file mode 100644 index ec5ab10..0000000 --- a/src/helper.js +++ /dev/null @@ -1,43 +0,0 @@ -function mkArray(lim) { - return Array.from(Array(lim), function(a, i) { return i; }); -} - -function extractErrorPositionFromErrorMsg(msg) { - const res = /Unexpected string in JSON at position (\d+)/.exec(msg); - return res ? parseInt(res[1], 10) : null; -} - -function extractErrorPlace(rawJson, position) { - const context = 20; - return rawJson.substring(position - context, position + context); -} - -function showFancySyntaxException(rawJson, e) { - var context = []; - var pos = extractErrorPositionFromErrorMsg(e.message); - if (pos) { - context.push(extractErrorPlace(rawJson, pos)); - context.push(mkArray(pos -1).map(function() { return '-'; }).join('') + '^'); - } - return [ - e.message, - context.join('\n') - ].join('\n'); -} - -function safeJsonParse(raw) { - try { - return JSON.parse(raw); - } - catch (e) { - throw new Error(H.showFancySyntaxException(raw, e)); - } -} - -module.exports = { - showFancySyntaxException: showFancySyntaxException, - mkArray: mkArray, - extractErrorPositionFromErrorMsg: extractErrorPositionFromErrorMsg, - extractErrorPlace: extractErrorPlace, - safeJsonParse: safeJsonParse -} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 0b19037..37cf6fe 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,7 @@ var _ = require('lodash'); var jsonCompat = require('json-schema-compatibility'); var HttpStatus = require('http-status-codes').getStatusText; var jp = require('jsonpath'); -var H = require('./helper'); +var bemfjp = require('better-error-message-for-json-parse'); exports.convert = function (raml) { //FIXME: @@ -351,7 +351,12 @@ function convertSchema(rawSchema) { assert(_.isString(rawSchema)); - var schema = H.safeJsonParse(rawSchema); + var schema = null; + try { + schema = bemfjp.safeJsonParse(rawSchema); + } catch (e) { + throw e; + } delete schema.id; delete schema.$schema; From ee33e8844dfca14dc5fa852dd209c290bb372408 Mon Sep 17 00:00:00 2001 From: Ivan Demchenko Date: Tue, 10 Apr 2018 19:37:49 +1000 Subject: [PATCH 4/4] cleaned up unnecessary try catch --- src/index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 37cf6fe..3dec81c 100644 --- a/src/index.js +++ b/src/index.js @@ -351,12 +351,7 @@ function convertSchema(rawSchema) { assert(_.isString(rawSchema)); - var schema = null; - try { - schema = bemfjp.safeJsonParse(rawSchema); - } catch (e) { - throw e; - } + var schema = bemfjp.safeJsonParse(rawSchema); delete schema.id; delete schema.$schema;