2

I'm unable to run any of the methods on this MyError class.

class MyError extends Error {
  constructor(message) {
    super()
    this.name = 'MyError'
    this.message = message
    this.stack = (new Error(message)).stack
    return this
  }
  doSomething () {
    return this.message + " dolphin"
  }
}

let myError = new MyError('Invalid Form.')

console.log(myError.doSomething())

For some reason this is giving me an error:

console.log(myError.doSomething());
                    ^

TypeError: myError.doSomething is not a function
twernt
  • 20,271
  • 5
  • 32
  • 41
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

1 Answers1

0

Found out you can't extend Error.

There's a package es6-error with an Error that you can extend.

import ExtendableError from 'es6-error';

class MyError extends ExtendableError {
  constructor(message) {
    super()
    this.name = 'MyError'
    this.message = message
    this.stack = (new Error(message)).stack
    return this
  }
  doSomething () {
    return this.message + " dolphin"
  }
}

let myError = new MyError('Invalid Form.')

console.log(myError.doSomething())
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • Might be worth including the reason why (Babel) in your answer with the link from your comment to make it explicit. – Andy Apr 01 '16 at 18:49