1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-07-01 07:50:06 +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:
hacksalot 2016-02-12 17:11:11 -05:00
parent daeffd27b5
commit b26799f9fc
7 changed files with 69 additions and 27 deletions

12
dist/cli/error.js vendored
View File

@ -213,9 +213,15 @@ Error-handling routines for HackMyResume.
if (SyntaxErrorEx.is(ex.inner)) { if (SyntaxErrorEx.is(ex.inner)) {
console.error(printf(M2C(this.msgs.readError.msg, 'red'), ex.file)); console.error(printf(M2C(this.msgs.readError.msg, 'red'), ex.file));
se = new SyntaxErrorEx(ex, ex.raw); se = new SyntaxErrorEx(ex, ex.raw);
msg = printf(M2C(this.msgs.parseError.msg, 'red'), se.line, se.col); if ((se.line != null) && (se.col != null)) {
} else if (ex.inner && ex.inner.line !== void 0 && ex.inner.col !== void 0) { msg = printf(M2C(this.msgs.parseError.msg[0], 'red'), se.line, se.col);
msg = printf(M2C(this.msgs.parseError.msg, 'red'), ex.inner.line, ex.inner.col); } else if (se.line != null) {
msg = printf(M2C(this.msgs.parseError.msg[1], 'red'), se.line);
} else {
msg = M2C(this.msgs.parseError.msg[2], 'red');
}
} else if (ex.inner && (ex.inner.line != null) && (ex.inner.col != null)) {
msg = printf(M2C(this.msgs.parseError.msg[0], 'red'), ex.inner.line, ex.inner.col);
} else { } else {
msg = ex; msg = ex;
} }

5
dist/cli/msg.yml vendored
View File

@ -81,7 +81,10 @@ errors:
readError: readError:
msg: Reading **???** resume: **%s** msg: Reading **???** resume: **%s**
parseError: parseError:
msg: Invalid or corrupt JSON on line %s column %s. msg:
- Invalid or corrupt JSON on line %s column %s.
- Invalid or corrupt JSON on line %s.
- Invalid or corrupt JSON.
invalidHelperUse: invalidHelperUse:
msg: "**Warning**: Incorrect use of the **%s** theme helper." msg: "**Warning**: Incorrect use of the **%s** theme helper."
fileSaveError: fileSaveError:

View File

@ -18,17 +18,31 @@ See: http://stackoverflow.com/q/13323356
(function() { (function() {
var SyntaxErrorEx; var SyntaxErrorEx;
SyntaxErrorEx = function(ex, rawData) { SyntaxErrorEx = (function() {
var JSONLint, colNum, lineNum, lint; function SyntaxErrorEx(ex, rawData) {
lineNum = null; var JSONLint, colNum, lineNum, lint, ref;
colNum = null; lineNum = null;
JSONLint = require('json-lint'); colNum = null;
lint = JSONLint(rawData, { JSONLint = require('json-lint');
comments: false lint = JSONLint(rawData, {
}); comments: false
this.line = lint.error ? lint.line : '???'; });
return this.col = lint.error ? lint.character : '???'; 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) { SyntaxErrorEx.is = function(ex) {
return ex instanceof SyntaxError; return ex instanceof SyntaxError;

View File

@ -61,6 +61,7 @@
"html": "0.0.10", "html": "0.0.10",
"is-my-json-valid": "^2.12.4", "is-my-json-valid": "^2.12.4",
"json-lint": "^0.1.0", "json-lint": "^0.1.0",
"jsonlint": "^1.6.2",
"lodash": "^3.10.1", "lodash": "^3.10.1",
"marked": "^0.3.5", "marked": "^0.3.5",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",

View File

@ -202,12 +202,17 @@ assembleError = ( ex ) ->
etype = 'custom' etype = 'custom'
when HMSTATUS.parseError when HMSTATUS.parseError
if SyntaxErrorEx.is( ex.inner ) if SyntaxErrorEx.is ex.inner
console.error printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) console.error printf( M2C(this.msgs.readError.msg, 'red'), ex.file )
se = new SyntaxErrorEx ex, ex.raw se = new SyntaxErrorEx ex, ex.raw
msg = printf M2C( this.msgs.parseError.msg, 'red' ), se.line, se.col if se.line? and se.col?
else if ex.inner && ex.inner.line != undefined && ex.inner.col != undefined msg = printf M2C( this.msgs.parseError.msg[0], 'red' ), se.line, se.col
msg = printf( M2C( this.msgs.parseError.msg, 'red' ), ex.inner.line, ex.inner.col) else if se.line?
msg = printf M2C( this.msgs.parseError.msg[1], 'red' ), se.line
else
msg = M2C @msgs.parseError.msg[2], 'red'
else if ex.inner && ex.inner.line? && ex.inner.col?
msg = printf( M2C( this.msgs.parseError.msg[0], 'red' ), ex.inner.line, ex.inner.col)
else else
msg = ex msg = ex
etype = 'error' etype = 'error'

View File

@ -81,7 +81,10 @@ errors:
readError: readError:
msg: Reading **???** resume: **%s** msg: Reading **???** resume: **%s**
parseError: parseError:
msg: Invalid or corrupt JSON on line %s column %s. msg:
- Invalid or corrupt JSON on line %s column %s.
- Invalid or corrupt JSON on line %s.
- Invalid or corrupt JSON.
invalidHelperUse: invalidHelperUse:
msg: "**Warning**: Incorrect use of the **%s** theme helper." msg: "**Warning**: Incorrect use of the **%s** theme helper."
fileSaveError: fileSaveError:

View File

@ -4,6 +4,8 @@ Definition of the SyntaxErrorEx class.
@license MIT. See LICENSE.md for details. @license MIT. See LICENSE.md for details.
### ###
###* ###*
Represents a SyntaxError exception with line and column info. Represents a SyntaxError exception with line and column info.
Collect syntax error information from the provided exception object. The Collect syntax error information from the provided exception object. The
@ -13,13 +15,21 @@ See: http://stackoverflow.com/q/13323356
@class SyntaxErrorEx @class SyntaxErrorEx
### ###
SyntaxErrorEx = ( ex, rawData ) -> class SyntaxErrorEx
lineNum = null constructor: ( ex, rawData ) ->
colNum = null lineNum = null
JSONLint = require 'json-lint' colNum = null
lint = JSONLint rawData, { comments: false } JSONLint = require 'json-lint'
this.line = if lint.error then lint.line else '???' lint = JSONLint rawData, { comments: false }
this.col = if lint.error then lint.character else '???' [@line, @col] = [lint.line, lint.character] if lint.error
if !lint.error
JSONLint = require 'jsonlint'
try
JSONLint.parse rawData
catch
@line = (/on line (\d+)/.exec _error)[1]
SyntaxErrorEx.is = ( ex ) -> ex instanceof SyntaxError SyntaxErrorEx.is = ( ex ) -> ex instanceof SyntaxError
module.exports = SyntaxErrorEx; module.exports = SyntaxErrorEx;