Skip to content
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

Consider using SyntaxError instead of Error in throwError #254

Open
jdanyow opened this issue Jul 26, 2023 · 0 comments
Open

Consider using SyntaxError instead of Error in throwError #254

jdanyow opened this issue Jul 26, 2023 · 0 comments

Comments

@jdanyow
Copy link

jdanyow commented Jul 26, 2023

A more specific exception might make it easier for jsep(...) callers to be confident the error is something that could be displayed to a user to help them correct their expression vs any other unexpected exception.

I realize the index and description properties could be used instead, however something more TypeScript friendly like extending SyntaxError could be a further improvement for folks catching these exceptions. If you agree, I'd love to open a PR.

export class JsepError extends SyntaxError {
  constructor(
    public readonly description: string;
    public readonly index: number;
  ) {
    super(`${description} at character ${index}`);
    this.name = this.constructor.name;
    if (typeof Error.captureStackTrace === 'function') {
      Error.captureStackTrace(this, this.constructor);
    }
  }
}
	/**
	 * throw error at index of the expression
	 * @param {string} message
	 * @throws
	 */
	throwError(message) {
-		const error = new Error(message + ' at character ' + this.index);
-		error.index = this.index;
-		error.description = message;
-		throw error;
+		throw new JsepError(message, this.index);
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant