New error function to quickly extend new JS errors #1052

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

@ericvicenti

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.

@puffnfresh

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

https://github.com/pufuwozu/bilby.js/blob/master/src/helpers.js#L258

@michaelficarra
Collaborator

@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
  F
@jashkenas
Owner

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