ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

Providing custom messages to OrThrow methods

Open HoldYourWaffle opened this issue 5 years ago • 2 comments

Is your feature request related to a problem? Please describe. It's currently not possible to provide specific error messages to the XXXOrThrow methods. This limits these "AST assertions" to generic error message that most likely won't help a user of your library.

To put it another way: the current error messages can only tell you what was expected, not why it was expected.

Describe the solution you'd like I'd love to add an optional message parameter to all the OrThrow methods. If this parameter isn't specified the current 'generic' messages should be used.

Describe alternatives you've considered You could try-catch the assertions and throw your own error but this is clunky and doesn't really work for multiple assertions.

HoldYourWaffle avatar Sep 29 '19 12:09 HoldYourWaffle

Yeah, this is a really good idea. It should be possible to add this feature to the library using a programmatic refactor (so please if someone goes to implement this don't do it manually)

dsherret avatar Oct 04 '19 00:10 dsherret

@dsherret @HoldYourWaffle I believe this idea could improve ts-morph based scripts a lot!

I prepared https://github.com/dsherret/ts-morph/pull/1327 to add the feature but I would need help (and feedback) to finalize it.

Preview

As you can see it would also show the end-user of the file and the location to understand which part of the AST triggered the exception.

jantimon avatar Sep 12 '22 13:09 jantimon