ts-morph
ts-morph copied to clipboard
Providing custom messages to OrThrow methods
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.
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 @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.
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.