Socket
Socket
Sign inDemoInstall

json-parse-even-better-errors

Package Overview
Dependencies
0
Maintainers
7
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-parse-even-better-errors

JSON.parse with context information on error


Version published
Weekly downloads
39M
decreased by-5.29%
Maintainers
7
Install size
9.46 kB
Created
Weekly downloads
 

Package description

What is json-parse-even-better-errors?

The json-parse-even-better-errors package is an npm package that provides an improved JSON.parse function. It offers better error messages than the default JSON.parse, making it easier to debug issues with JSON data. It can pinpoint the exact location and cause of the error in the JSON string.

What are json-parse-even-better-errors's main functionalities?

Improved error messages

This feature enhances the error messages provided by JSON.parse. In the code sample, if the JSON is invalid, it will throw an error with a message that includes the position and reason for the failure, making it easier to identify and fix the issue.

const parseJSON = require('json-parse-even-better-errors');
try {
  const obj = parseJSON('{"foo": "bar", "baz": }');
} catch (e) {
  console.error(e.message);
}

Other packages similar to json-parse-even-better-errors

Readme

Source

json-parse-even-better-errors npm version license Travis AppVeyor Coverage Status

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.

Install

$ npm install --save json-parse-even-better-errors

Table of Contents

Example

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

Features

  • 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.

API

parse(txt, reviver = null, context = 20)

Works just like JSON.parse, but will include a bit more information when an error happens. This throws a JSONParseError.

parse.noExceptions(txt, reviver = null)

Works just like JSON.parse, but will return undefined rather than throwing an error.

class JSONParseError(er, text, context = 20, caller = null)

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.

Keywords

FAQs

Last updated on 05 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc