First option is a good idea, but frankly the authors of this method probably didn't consider it that far. They just wrote the method to assume everything thrown at it would be a string (if not a regex) and the exceptions got coerced into one. Which is how most JS code will work when you don't actively handle the wrong type parameters.
Second option is still a problem, JS has error handling but you have to actively use it. Wrapping every method of your code in try-catch (or multiple try-catches, since we wouldn't want one error somewhere to break unrelated code) ends up becoming boilerplate and bureaucratic.
What I mean by built-in error handling that would be better than this, is I would rather the language had an automatic
try{}catch{log(error)}
around every method unless otherwise specified. Other stuff that relied on the method would still break and propagate those errors, but it would be breaking anyway.
View all comments
Show parent comment
11
u/LucasRuby Mar 21 '24
First option is a good idea, but frankly the authors of this method probably didn't consider it that far. They just wrote the method to assume everything thrown at it would be a string (if not a regex) and the exceptions got coerced into one. Which is how most JS code will work when you don't actively handle the wrong type parameters.
Second option is still a problem, JS has error handling but you have to actively use it. Wrapping every method of your code in try-catch (or multiple try-catches, since we wouldn't want one error somewhere to break unrelated code) ends up becoming boilerplate and bureaucratic.