1
    if (win.NS && win.NS.molist && win.NS.molist.utility) {
        NS = win.NS;
        NS.molist.dom = true;
    } else {
        throw "dom requires utility module";
    }

For the snippet above, what is the proper way to throw an error, if win.NS.molist.utility does not exist?

Can I just throw up the text I want displayed to be shown in the debugger?

Perhaps, I should use one of the built in error types?

Maybe a new TypeError, I'm not sure as there are many global Errors objects.

  • throw new Error("dom requires utility module"); I don't understand your problem ([error types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types)) – ultima_rat0 Jul 24 '13 at 21:50
  • [What's a good way to extend Error in JavaScript?](http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript) – Givi Jul 24 '13 at 21:52

1 Answers1

3

You can throw a completely custom Error.

function ModuleNotFoundError(message) {
  this.name = "ModuleNotFound";
  this.message = message || "dom does not have this module";
}
ModuleNotFoundError.prototype = new Error();
ModuleNotFoundError.prototype.constructor = ModuleNotFoundError;

throw new ModuleNotFoundError('win.NS.molist.utility');
Paul S.
  • 64,864
  • 9
  • 122
  • 138