mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2025-05-02 20:37:08 +01:00
Improve JSON error handling.
Add support for detection of invalid line breaks in JSON string values. Fixes #137. Could be improved to fetch the column number and drop the messy grabbing of the line number from the exception message via regex, but currently the "jsonlint" library (not to be confused with "json-lint") only emits an error string. Since this is also the library that drives http://jsonlint.com, we'll accept the messy regex in return for more robust error checking when our default json-lint path fails. All of the above only necessary because standard JSON.parse error handling is broken in all environments. : )
This commit is contained in:
36
dist/utils/syntax-error-ex.js
vendored
36
dist/utils/syntax-error-ex.js
vendored
@ -18,17 +18,31 @@ See: http://stackoverflow.com/q/13323356
|
||||
(function() {
|
||||
var SyntaxErrorEx;
|
||||
|
||||
SyntaxErrorEx = function(ex, rawData) {
|
||||
var JSONLint, colNum, lineNum, lint;
|
||||
lineNum = null;
|
||||
colNum = null;
|
||||
JSONLint = require('json-lint');
|
||||
lint = JSONLint(rawData, {
|
||||
comments: false
|
||||
});
|
||||
this.line = lint.error ? lint.line : '???';
|
||||
return this.col = lint.error ? lint.character : '???';
|
||||
};
|
||||
SyntaxErrorEx = (function() {
|
||||
function SyntaxErrorEx(ex, rawData) {
|
||||
var JSONLint, colNum, lineNum, lint, ref;
|
||||
lineNum = null;
|
||||
colNum = null;
|
||||
JSONLint = require('json-lint');
|
||||
lint = JSONLint(rawData, {
|
||||
comments: false
|
||||
});
|
||||
if (lint.error) {
|
||||
ref = [lint.line, lint.character], this.line = ref[0], this.col = ref[1];
|
||||
}
|
||||
if (!lint.error) {
|
||||
JSONLint = require('jsonlint');
|
||||
try {
|
||||
JSONLint.parse(rawData);
|
||||
} catch (_error) {
|
||||
this.line = (/on line (\d+)/.exec(_error))[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return SyntaxErrorEx;
|
||||
|
||||
})();
|
||||
|
||||
SyntaxErrorEx.is = function(ex) {
|
||||
return ex instanceof SyntaxError;
|
||||
|
Reference in New Issue
Block a user