Skip to content

Commit

Permalink
Simplify JSONError
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 9, 2023
1 parent 7230975 commit 10b10b6
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@ import {codeFrameColumns} from '@babel/code-frame';
import indexToPosition from 'index-to-position';

export class JSONError extends Error {
name = 'JSONError';
fileName;
codeFrame;
rawCodeFrame;
#message;

constructor(message) {
super(message);

let _message = message instanceof Error
? message.message
: message;

Object.defineProperty(this, 'message', {
configurable: true,
enumerable: false,
get() {
return `${_message}${this.fileName ? ` in ${this.fileName}` : ''}${this.codeFrame ? `\n\n${this.codeFrame}\n` : ''}`;
},
set(value) {
_message = value;
},
});

this.name = 'JSONError';

if (Error.captureStackTrace) {
Error.captureStackTrace(this, JSONError);
}
// We can't pass message to `super()`, otherwise the message getter and setter will be overridden
// https://262.ecma-international.org/14.0/#sec-error-message
super();
this.#message = message;
Error.captureStackTrace?.(this, JSONError);
}

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

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

Expand Down

0 comments on commit 10b10b6

Please sign in to comment.