Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.setPrototypeOf() method is missing / not transpiled correctly #264

Closed
pe1pip opened this issue Apr 19, 2024 · 10 comments
Closed

Object.setPrototypeOf() method is missing / not transpiled correctly #264

pe1pip opened this issue Apr 19, 2024 · 10 comments
Assignees

Comments

@pe1pip
Copy link
Contributor

pe1pip commented Apr 19, 2024

Description

I want to extend the Error class. Apparently the transpilation of the code is incorrect:

export class NsxtError extends Error {
  public readonly cause: ErrorCause
  constructor (p: string, m: string, cause: ErrorCause) {
    super(`${p}${m}`)
    Object.setPrototypeOf(this, new.target.prototype) // restore this object
    this.cause = cause
  }
}

Is transpiled to:

    var NsxtError = /** @class */ (function (_super) {
        tslib_1.__extends(NsxtError, _super);
        function NsxtError(p, m, cause) {
            var _newTarget = this.constructor;
            var _this = _super.call(this, "" + p + m) || this;
            Object.setPrototypeOf(_this, _newTarget.prototype); // restore this object
            _this.cause = cause;
            return _this;
        }
        return NsxtError;
    }(Error));

and it should be:

    var NsxtError = /** @class */ (function (_super) {
        tslib_1.__extends(NsxtError, _super);
        function NsxtError(p, m, cause) {
            var _newTarget = this.constructor;
            var _this = _super.call(this, "" + p + m) || this;
            _this.__proto__ = _newTarget.prototype
            _this.cause = cause;
            return _this;
        }
        return NsxtError;
    }(Error));

Alternatives

don't know.

Additional Context

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label May 20, 2024
@pe1pip
Copy link
Contributor Author

pe1pip commented May 22, 2024

would be nice if somebody could look at this

@github-actions github-actions bot removed the Stale label May 23, 2024
@VenelinBakalov
Copy link
Contributor

Hi @pe1pip , could you share what issue or error you are encountering in vRO with this setup? It should be possible to extend the error object without an issue. Do you need to explicitly call the "Object.setPrototypeOf" function?
Here is one sample of error inheritance:

export class MissingObjectError extends Error {
    constructor(name: string, entityType: string) {
        const message = "Entity with name '" + name + "' not found in vCenter.";
        super(message);
        this.name = MissingObjectError.name;
    }
}

@VenelinBakalov
Copy link
Contributor

I am not sure in this case of inheritance whether you will really need the Object.setPrototypeOf() call but we should be able to extend and add this function.

@pe1pip
Copy link
Contributor Author

pe1pip commented May 27, 2024

the problem arises if you want additional properties in the Error object, in this case the cause property:

export class NsxtError extends Error {
  public readonly cause: ErrorCause
  constructor (p: string, m: string, cause: ErrorCause) {
    super(`${p}${m}`)
    Object.setPrototypeOf(this, new.target.prototype) // restore this object
    this.cause = cause
  }
}

@VenelinBakalov
Copy link
Contributor

Interesting..in theory "extends" keyword should be enough. However, in my tests I found that without Object.setPrototypeOf() everything seems to work for the error but if you log err.constructor.name it logs "Error". But if you add the setPrototypeOf() call as in your sample err.constructor.name will log NsxtError.

I think I managed to get it running at least with a simple test. If everything is ok I will open a PR tomorrow but it would be great to double check on your side as well

@pe1pip
Copy link
Contributor Author

pe1pip commented May 28, 2024

@VenelinBakalov
Copy link
Contributor

@pe1pip whenever you have time could you check and possibly test on your side as well: https://github.com/vmware/build-tools-for-vmware-aria/pull/277/files

@pe1pip
Copy link
Contributor Author

pe1pip commented May 28, 2024

Hi @VenelinBakalov , I have a build error in vropkg, this is probably something in my environment, but I have no time to investigate. The PR looks quite ok, so I have little doubt that it works.

@VenelinBakalov
Copy link
Contributor

Got it, thanks for pointing out this issue and adding the related documentation links, that was really helpful!

Michaelpalacce added a commit that referenced this issue Jun 12, 2024
Regarding #264

Signed-off-by: Stefan Genov <stefan.genov@broadcom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants