Skip to content
Snippets Groups Projects
Commit 2686e0c2 authored by Hari Govind S's avatar Hari Govind S
Browse files

New upstream version 1.0.2

parent 26dccf09
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,16 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
<a name="1.0.2"></a>
## [1.0.2](https://github.com/zkat/json-parse-better-errors/compare/v1.0.1...v1.0.2) (2018-03-30)
### Bug Fixes
* **messages:** More friendly messages for non-string ([#1](https://github.com/zkat/json-parse-better-errors/issues/1)) ([a476d42](https://github.com/zkat/json-parse-better-errors/commit/a476d42))
<a name="1.0.1"></a>
## [1.0.1](https://github.com/zkat/json-parse-better-errors/compare/v1.0.0...v1.0.1) (2017-08-16)
......
# json-parse-better-errors [![npm version](https://img.shields.io/npm/v/json-parse-better-errors.svg)](https://npm.im/json-parse-better-errors) [![license](https://img.shields.io/npm/l/json-parse-better-errors.svg)](https://npm.im/json-parse-better-errors) [![Travis](https://img.shields.io/travis/zkat/json-parse-better-errors.svg)](https://travis-ci.org/zkat/json-parse-better-errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/zkat/json-parse-better-errors?svg=true)](https://ci.appveyor.com/project/zkat/json-parse-better-errors) [![Coverage Status](https://coveralls.io/repos/github/zkat/json-parse-better-errors/badge.svg?branch=latest)](https://coveralls.io/github/zkat/json-parse-better-errors?branch=latest)
[`json-parse-better-errors`](https://github.com/zkat/json-parse-better-errors) is a Node.js library for managing
local key and content address caches. It's really fast, really good at
concurrency, and it will never give you corrupted data, even if cache files
get corrupted or manipulated.
It was originally written to be used as [npm](https://npm.im)'s local cache, but
can just as easily be used on its own
_Translations: [español](README.es.md)_
[`json-parse-better-errors`](https://github.com/zkat/json-parse-better-errors) is a Node.js library for
getting nicer errors out of `JSON.parse()`, including context and position of the parse errors.
## Install
......@@ -37,7 +30,7 @@ parseJson('garbage') // more useful error message
### Contributing
The json-parse-better-errors team enthusiastically welcomes contributions and project participation! There's a bunch of things you can do if you want to contribute! The [Contributor Guide](CONTRIBUTING.md) has all the information you need for everything from reporting bugs to contributing entire new features. Please don't hesitate to jump in if you'd like to, or even ask us questions if something isn't clear.
The npm team enthusiastically welcomes contributions and project participation! There's a bunch of things you can do if you want to contribute! The [Contributor Guide](CONTRIBUTING.md) has all the information you need for everything from reporting bugs to contributing entire new features. Please don't hesitate to jump in if you'd like to, or even ask us questions if something isn't clear.
All participants and maintainers in this project are expected to follow [Code of Conduct](CODE_OF_CONDUCT.md), and just generally be excellent to each other.
......
......@@ -6,6 +6,12 @@ function parseJson (txt, reviver, context) {
try {
return JSON.parse(txt, reviver)
} catch (e) {
if (typeof txt !== 'string') {
const isEmptyArray = Array.isArray(txt) && txt.length === 0
const errorMessage = 'Cannot parse ' +
(isEmptyArray ? 'an empty array' : String(txt))
throw new TypeError(errorMessage)
}
const syntaxErr = e.message.match(/^Unexpected token.*position\s+(\d+)/i)
const errIdx = syntaxErr
? +syntaxErr[1]
......
{
"name": "json-parse-better-errors",
"version": "1.0.1",
"version": "1.0.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......
{
"name": "json-parse-better-errors",
"version": "1.0.1",
"version": "1.0.2",
"description": "JSON.parse with context information on error",
"main": "index.js",
"files": [
......@@ -22,7 +22,7 @@
],
"author": {
"name": "Kat Marchán",
"email": "kzm@sykosomatic.org",
"email": "kzm@zkat.tech",
"twitter": "maybekatz"
},
"license": "MIT",
......
......@@ -14,3 +14,70 @@ test('parses JSON', t => {
t.deepEqual(JSON.parse(data), parseJson(data), 'does the same thing')
t.done()
})
test('throws SyntaxError for unexpected token', t => {
const data = 'foo'
t.throws(
() => parseJson(data),
new SyntaxError('Unexpected token o in JSON at position 1 while parsing near \'foo\'')
)
t.done()
})
test('throws SyntaxError for unexpected end of JSON', t => {
const data = '{"foo: bar}'
t.throws(
() => parseJson(data),
new SyntaxError('Unexpected end of JSON input while parsing near \'{"foo: bar}\'')
)
t.done()
})
test('throws SyntaxError for unexpected number', t => {
const data = '[[1,2],{3,3,3,3,3}]'
t.throws(
() => parseJson(data),
new SyntaxError('Unexpected number in JSON at position 8')
)
t.done()
})
test('SyntaxError with less context (limited start)', t => {
const data = '{"6543210'
t.throws(
() => parseJson(data, null, 3),
new SyntaxError('Unexpected end of JSON input while parsing near \'...3210\''))
t.done()
})
test('SyntaxError with less context (limited end)', t => {
const data = 'abcde'
t.throws(
() => parseJson(data, null, 2),
new SyntaxError('Unexpected token a in JSON at position 0 while parsing near \'ab...\''))
t.done()
})
test('throws TypeError for undefined', t => {
t.throws(
() => parseJson(undefined),
new TypeError('Cannot parse undefined')
)
t.done()
})
test('throws TypeError for non-strings', t => {
t.throws(
() => parseJson(new Map()),
new TypeError('Cannot parse [object Map]')
)
t.done()
})
test('throws TypeError for empty arrays', t => {
t.throws(
() => parseJson([]),
new TypeError('Cannot parse an empty array')
)
t.done()
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment