Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
wrap JSON.parse itself
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Demchenko committed Mar 9, 2018
1 parent ab2974f commit ab8ff7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 1 addition & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ab8ff7e

Please sign in to comment.