Skip to content

Throw an appropriate Error (an instance of Error class), instead of Object #2400

Closed
@RomanDavlyatshin

Description

@RomanDavlyatshin
  • I have first restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

// the code you're trying to parse
// any invalid TS code
hello {{{{}
// the code you're using to do the parse of the aforementioned code
import { parse, TSESTreeOptions } from '@typescript-eslint/typescript-estree';

export default function (source, options: TSESTreeOptions = {}, defaultOptions: TSESTreeOptions = {
  comment: false,
  jsx: true,
  loc: true,
  range: true
}) {
  return parse(source, {
    ...defaultOptions,
    ...options
  });
}

Expected Result
Error (that is an instance of Error) being thrown, with stack trace info:

{
   message: '';' expected',
   stack: *error stack*,
   // I suggest to extend default Error to add additional params
   details: {
     column: 6,
     index: 43,
     lineNumber: 2,
   }
}

Actual Result
I'm getting an Object instead of Error

{
  index: 43,
  lineNumber: 2,
  column: 6,
  message: "';' expected.",
}

Additional Info
Throwing anything besides Error is a bad practice because a stack trace is lost.
The stack trace is valuable info.
If it's crucial to add additional info I suggest extending the default Error class with the complementary field and put that info there.

At first glance, it looks like it could be easily done (create extended Error class and modify createError method to return new CustomError({/*object containing details*/}) //error name is a subject to the discussion)
I would like to submit a PR if no one minds, but I would be glad to hear any critique.

P.S. Of course, I could just wrap all calls to parse into try ... catch and rethrow, but I would like to see that fixed in-library

Versions

package version
@typescript-eslint/typescript-estree 2.34.0
TypeScript 3.9.5
node 14.4.0

Metadata

Metadata

Assignees

Labels

breaking changeThis change will require a new major version to be releasedenhancementNew feature or requesthas prthere is a PR raised to close thispackage: typescript-estreeIssues related to @typescript-eslint/typescript-estree

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions