Skip to content

Add orignal error to JSONError.cause #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,56 @@ const getCodePoint = character => `\\u{${character.codePointAt(0).toString(16)}}
export class JSONError extends Error {
name = 'JSONError';
fileName;
codeFrame;
rawCodeFrame;
#input;
#jsonParseError;
#message;
#codeFrame;
#rawCodeFrame;

constructor(message) {
constructor({jsonParseError, fileName, input}) {
// We cannot pass message to `super()`, otherwise the message accessor will be overridden.
// https://262.ecma-international.org/14.0/#sec-error-message
super();
super(undefined, {cause: jsonParseError});

this.#input = input;
this.#jsonParseError = jsonParseError;
this.fileName = fileName;

this.#message = message;
Error.captureStackTrace?.(this, JSONError);
}

get message() {
const {fileName, codeFrame} = this;
return `${this.#message}${fileName ? ` in ${fileName}` : ''}${codeFrame ? `\n\n${codeFrame}\n` : ''}`;
this.#message ??= `${addCodePointToUnexpectedToken(this.#jsonParseError.message)}${this.#input === '' ? ' while parsing empty string' : ''}`;

const {codeFrame} = this;
return `${this.#message}${this.fileName ? ` in ${this.fileName}` : ''}${codeFrame ? `\n\n${codeFrame}\n` : ''}`;
}

set message(message) {
this.#message = message;
}
}

const generateCodeFrame = (string, location, highlightCode = true) =>
codeFrameColumns(string, {start: location}, {highlightCode});
#getCodeFrame(highlightCode) {
const input = this.#input;

const location = getErrorLocation(input, this.#jsonParseError.message);
if (!location) {
return;
}

return codeFrameColumns(input, {start: location}, {highlightCode});
}

get codeFrame() {
this.#codeFrame ??= this.#getCodeFrame(/* highlightCode */ true);
return this.#codeFrame;
}

get rawCodeFrame() {
this.#rawCodeFrame ??= this.#getCodeFrame(/* highlightCode */ false);
return this.#rawCodeFrame;
}
}

const getErrorLocation = (string, message) => {
const match = message.match(/in JSON at position (?<index>\d+)(?: \(line (?<line>\d+) column (?<column>\d+)\))?$/);
Expand Down Expand Up @@ -68,29 +93,13 @@ export default function parseJson(string, reviver, fileName) {
reviver = undefined;
}

let message;
try {
return JSON.parse(string, reviver);
} catch (error) {
message = error.message;
throw new JSONError({
jsonParseError: error,
fileName,
input: string,
});
}

let location;
if (string) {
location = getErrorLocation(string, message);
message = addCodePointToUnexpectedToken(message);
} else {
message += ' while parsing empty string';
}

const jsonError = new JSONError(message);

jsonError.fileName = fileName;

if (location) {
jsonError.codeFrame = generateCodeFrame(string, location);
jsonError.rawCodeFrame = generateCodeFrame(string, location, /* highlightCode */ false);
}

throw jsonError;
}
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ test('main', t => {
}, {
message: errorMessageRegexWithFileName,
});

{
let nativeJsonParseError;
try {
JSON.parse(INVALID_JSON_STRING);
} catch (error) {
nativeJsonParseError = error;
}

let jsonError;
try {
parseJson(INVALID_JSON_STRING);
} catch (error) {
jsonError = error;
}

t.is(nativeJsonParseError.name, 'SyntaxError');
t.deepEqual(nativeJsonParseError, jsonError.cause);
}
});

test('throws exported error error', t => {
Expand Down