Skip to content

Instantly share code, notes, and snippets.

@xdissent
Created May 27, 2014 16:03
Show Gist options
  • Save xdissent/d20bbdd57ca16b3d86b5 to your computer and use it in GitHub Desktop.
Save xdissent/d20bbdd57ca16b3d86b5 to your computer and use it in GitHub Desktop.
Extendable native Error subclasses in Coffeescript
ok = (ok) -> console.log if ok then 'ok' else 'FAIL'
FnError = (message) ->
@name = 'FnError'
@message = message
@stack = (new Error).stack
FnError:: = new Error
fne = new FnError 'fn'
ok fne.name is 'FnError'
ok fne.message is 'fn'
ok fne instanceof FnError
ok fne instanceof Error
class ClassError
constructor: (message) ->
@name = @constructor.name
@message = message
@stack = (new Error).stack
@:: = new Error
@::constructor = @
ce = new ClassError 'class'
ok ce.name is 'ClassError'
ok ce.message is 'class'
ok ce instanceof ClassError
ok ce instanceof Error
class SubClassError extends ClassError
sce = new SubClassError 'subclass'
ok sce.name is 'SubClassError'
ok sce.message is 'subclass'
ok sce instanceof SubClassError
ok sce instanceof ClassError
ok sce instanceof Error
class AnotherSubClassError extends SubClassError
constructor: -> super 'custom constructor'
asce = new AnotherSubClassError 'anothersubclass'
ok asce.name is 'AnotherSubClassError'
ok asce.message is 'custom constructor'
ok asce instanceof AnotherSubClassError
ok asce instanceof SubClassError
ok asce instanceof ClassError
ok asce instanceof Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment