0

I have made a error class hierarchy, starting with a BaseError class that is purely meant for subclassing. I have set the prototype using Object.create(Error.prototype) and I am rewriting the stack using the name of the current subclass. The name is set in a method called configure that needs to be implemented by the subclasses.

The problem is that although I explicitly add the configure method to the prototype of BaseError it is not actually accessible in the prototype chain.

Failing

I think it is somehow related to the __proto__ instance property vs the prototype attribute.

__proto__

This is the code (transpiled from the Typescript definition)

var BaseError = (function () {
    function BaseError(msg) {
        var err = Error.apply(null, arguments);
        this.message = err.message;

        this.configure();

        if (err.stack) {
            this.stack = rewriteStack(err.stack, this.name);
        }

        if (typeof this.name === 'undefined')
            throw new NotImplementedError('must set "name" property in the configure call');
    }

    return BaseError;
})();

Framework.BaseError = BaseError;

// weird stuff need to happen to make BaseError pass an "instanceof Error"
BaseError.prototype = Object.create(Error.prototype);

BaseError.prototype.configure = function () {
    throw new NotImplementedError(+' This method must be implemented in the overriding class!');
};

1 Answer 1

1

You're doing Object.create(BaseError), not Object.create(BaseError.prototype) as you should.

Sign up to request clarification or add additional context in comments.

2 Comments

Jeebus. Not the first time I have done something like that, either. Thanks. I see someone giving me a close vote. What do you think? Keep it up or not?
It was mine :-) I'm glad I could help, but I don't think the question will be helpful to others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.