1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-10 15:57:07 +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

View File

@ -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
@ -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;