How about this solution?
Instead of throwing your custom Error using:
throw new MyError("Oops!");
You would wrap the Error object (kind of like a Decorator):
throw new MyError(new Error("Oops!"));
This makes sure all of the attributes are correct, such as the stack, fileName lineNumber, et cetera.
All you have to do then is either copy over the attributes, or define getters for them. Here is an example using getters (IE9):
function MyError(wrapped)
{
this.wrapped = wrapped;
this.wrapped.name = 'MyError';
}
function wrap(attr)
{
Object.defineProperty(MyError.prototype, attr, {
get: function()
{
return this.wrapped[attr];
}
});
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;
wrap('name');
wrap('message');
wrap('stack');
wrap('fileName');
wrap('lineNumber');
wrap('columnNumber');
MyError.prototype.toString = function()
{
return this.wrapped.toString();
};