json-parse-even-better-errors
is a Node.js library for
getting nicer errors out of JSON.parse()
, including context and position of the parse errors.
$ npm install --save json-parse-even-better-errors
const parseJson = require('json-parse-even-better-errors')
parseJson('"foo"') // returns the string 'foo'
parseJson('garbage') // more useful error message
parseJson.noExceptions('garbage') // returns undefined
- Like JSON.parse, but the errors are better.
- Strips a leading byte-order-mark that you sometimes get reading files.
- Has a
noExceptions
method that returns undefined rather than throwing.
Works just like JSON.parse
, but will include a bit more information when an
error happens. This throws a JSONParseError
.
Works just like JSON.parse
, but will return undefined
rather than
throwing an error.
Extends the JavaScript SyntaxError
class to parse the message and provide
better metadata.
Pass in the error thrown by the built-in JSON.parse
, and the text being
parsed, and it'll parse out the bits needed to be helpful.
context
defaults to 20.
Set a caller
function to trim internal implementation details out of the
stack trace. When calling parseJson
, this is set to the parseJson
function. If not set, then the constructor defaults to itself, so the
stack trace will point to the spot where you call new JSONParseError
.