Skip to content

Commit

Permalink
Improve JSON error handling.
Browse files Browse the repository at this point in the history
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. : )
  • Loading branch information
hacksalot committed Feb 12, 2016
1 parent daeffd2 commit b26799f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 27 deletions.
12 changes: 9 additions & 3 deletions dist/cli/error.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion dist/cli/msg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ errors:
readError:
msg: Reading **???** resume: **%s**
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:
msg: "**Warning**: Incorrect use of the **%s** theme helper."
fileSaveError:
Expand Down
36 changes: 25 additions & 11 deletions dist/utils/syntax-error-ex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"html": "0.0.10",
"is-my-json-valid": "^2.12.4",
"json-lint": "^0.1.0",
"jsonlint": "^1.6.2",
"lodash": "^3.10.1",
"marked": "^0.3.5",
"mkdirp": "^0.5.1",
Expand Down
13 changes: 9 additions & 4 deletions src/cli/error.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,17 @@ assembleError = ( ex ) ->
etype = 'custom'

when HMSTATUS.parseError
if SyntaxErrorEx.is( ex.inner )
if SyntaxErrorEx.is ex.inner
console.error printf( M2C(this.msgs.readError.msg, 'red'), ex.file )
se = new SyntaxErrorEx ex, ex.raw
msg = printf M2C( this.msgs.parseError.msg, 'red' ), se.line, se.col
else if ex.inner && ex.inner.line != undefined && ex.inner.col != undefined
msg = printf( M2C( this.msgs.parseError.msg, 'red' ), ex.inner.line, ex.inner.col)
if se.line? and se.col?
msg = printf M2C( this.msgs.parseError.msg[0], 'red' ), se.line, se.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
msg = ex
etype = 'error'
Expand Down
5 changes: 4 additions & 1 deletion src/cli/msg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ errors:
readError:
msg: Reading **???** resume: **%s**
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:
msg: "**Warning**: Incorrect use of the **%s** theme helper."
fileSaveError:
Expand Down
24 changes: 17 additions & 7 deletions src/utils/syntax-error-ex.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Definition of the SyntaxErrorEx class.
@license MIT. See LICENSE.md for details.
###



###*
Represents a SyntaxError exception with line and column info.
Collect syntax error information from the provided exception object. The
Expand All @@ -13,13 +15,21 @@ See: http://stackoverflow.com/q/13323356
@class SyntaxErrorEx
###

SyntaxErrorEx = ( ex, rawData ) ->
lineNum = null
colNum = null
JSONLint = require 'json-lint'
lint = JSONLint rawData, { comments: false }
this.line = if lint.error then lint.line else '???'
this.col = if lint.error then lint.character else '???'
class SyntaxErrorEx
constructor: ( ex, rawData ) ->
lineNum = null
colNum = null
JSONLint = require 'json-lint'
lint = JSONLint rawData, { comments: false }
[@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
module.exports = SyntaxErrorEx;

0 comments on commit b26799f

Please sign in to comment.