I'm defining custom JavaScript exceptions like the code below. Is this proper? Isn't there a shorter way?
function InvalidModuleError(moduleName) {
TypeError.apply(this);
this.message = "module '" + moduleName + "' doesn't export any definitions";
this.name = 'InvalidModuleError';
};
InvalidModuleError.prototype = Object.create(TypeError.prototype);
function DuplicateModuleError(moduleName) {
TypeError.apply(this);
this.message = "module '" + moduleName + "' is already defined";
this.name = 'DuplicateModuleError';
};
DuplicateModuleError.prototype = Object.create(TypeError.prototype);
Edit: Eventually, after Oriol's suggestion, I've made a function that generates exceptions and looks like this:
function makeException(parentObject, parentClass, name, message) {
var shortName = name.split('.');
shortName = shortName[shortName.length - 1];
parentObject[shortName] = function() {
this.message = (typeof message === 'function') ? message.apply(null, arguments) : message;
};
parentObject[shortName].prototype = Object.create(parentClass.prototype, {'name': {'value': name}});
}