Sha256: a86f431b9c79a88034ddb14831d061527d3bf7a98a4152698aa51cd35909db81

Contents?: true

Size: 1.3 KB

Versions: 17

Compression:

Stored size: 1.3 KB

Contents

'use strict';
const errorEx = require('error-ex');
const fallback = require('json-parse-even-better-errors');
const {default: LinesAndColumns} = require('lines-and-columns');
const {codeFrameColumns} = require('@babel/code-frame');

const JSONError = errorEx('JSONError', {
	fileName: errorEx.append('in %s'),
	codeFrame: errorEx.append('\n\n%s\n')
});

const parseJson = (string, reviver, filename) => {
	if (typeof reviver === 'string') {
		filename = reviver;
		reviver = null;
	}

	try {
		try {
			return JSON.parse(string, reviver);
		} catch (error) {
			fallback(string, reviver);
			throw error;
		}
	} catch (error) {
		error.message = error.message.replace(/\n/g, '');
		const indexMatch = error.message.match(/in JSON at position (\d+) while parsing/);

		const jsonError = new JSONError(error);
		if (filename) {
			jsonError.fileName = filename;
		}

		if (indexMatch && indexMatch.length > 0) {
			const lines = new LinesAndColumns(string);
			const index = Number(indexMatch[1]);
			const location = lines.locationForIndex(index);

			const codeFrame = codeFrameColumns(
				string,
				{start: {line: location.line + 1, column: location.column + 1}},
				{highlightCode: true}
			);

			jsonError.codeFrame = codeFrame;
		}

		throw jsonError;
	}
};

parseJson.JSONError = JSONError;

module.exports = parseJson;

Version data entries

17 entries across 16 versions & 6 rubygems

Version Path
rapid_stack-0.2.0 templates/frontend/node_modules/parse-json/index.js
rapid_stack-0.1.1 templates/FrontEnd/node_modules/parse-json/index.js
rapid_stack-0.1.0 templates/FrontEnd/node_modules/parse-json/index.js
trusty-cms-6.3.1 node_modules/parse-json/index.js
pcp-server-ruby-sdk-0.0.6 node_modules/parse-json/index.js
pcp-server-ruby-sdk-0.1.0 node_modules/parse-json/index.js
disco_app-0.16.1 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
disco_app-0.15.2 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
disco_app-0.18.4 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
disco_app-0.18.1 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
disco_app-0.14.0 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/babel-plugin-macros/node_modules/parse-json/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/babel-plugin-macros/node_modules/parse-json/index.js