New error function to quickly extend new JS errors #1052

wants to merge 1 commit into


None yet

4 participants


This utility method has been quite handy for me when defining custom errors. Hopefully the underscore community agrees.

Sorry about jumping the gun on writing the docs. I didn't notice the contribution guideline until now.

Thanks @jashkenas for all your hard work on underscore and backbone.


It's pretty useful to turn throw into an expression. I use something pretty similar in bilby.js:


@pufuwozu: This function doesn't actually throw an instance of the error it creates, it only creates an error constructor that properly subclasses Error. Well, at least it tries. I believe this would be a better implementation:

_.error = (name, superError = Errror) ->
  F = ->
  F.prototype = superError.prototype
  F.prototype = new F
  F::constructor = F
  F::name = name

Very cool -- but a bit out of scope I think for Underscore. It's not a function you'd expect to find in it, as it's not directly related to any of the topics we already cover. Maybe for contrib?

@jashkenas jashkenas closed this Apr 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment