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

Can not throw an error inside ObjectWrap contructor. #789

Closed
Ayzrian opened this issue Aug 10, 2020 · 2 comments
Closed

Can not throw an error inside ObjectWrap contructor. #789

Ayzrian opened this issue Aug 10, 2020 · 2 comments

Comments

@Ayzrian
Copy link

@Ayzrian Ayzrian commented Aug 10, 2020

I have a class that extends ObjectWrap. Here it's declaration.

class NodeOSUserRegistrationMsg: public Napi::ObjectWrap<NodeOSUserRegistrationMsg> {
    public:
        static std::string innerTypeName;
        static Napi::FunctionReference constructor;
        static void Init(Napi::Env env, Napi::Object exports);

        FOSUserRegistrationMsg* GetInternalInstance();

        NodeOSUserRegistrationMsg(const Napi::CallbackInfo& info);
        ~NodeOSUserRegistrationMsg();

        Napi::Value GetClientAppToken(const Napi::CallbackInfo& info);
        Napi::Value GetOsUserSid(const Napi::CallbackInfo& info);

        void SetClientAppToken(const Napi::CallbackInfo& info, const Napi::Value& value);
        void SetOsUserSid(const Napi::CallbackInfo& info, const Napi::Value& value);

    private:
        FOSUserRegistrationMsg* implementation;
};

I want to validate the constructor arguments. In the following way.

NodeOSUserRegistrationMsg::NodeOSUserRegistrationMsg(const Napi::CallbackInfo& info): Napi::ObjectWrap<NodeOSUserRegistrationMsg>(info) {
    if (info.Length() == 1 & info[0].IsExternal()) {
        this->implementation = info[0].As<Napi::External<FOSUserRegistrationMsg>>().Data();
    } else if (info.Length() == 0) {
        this->implementation = new FOSUserRegistrationMsg(); 
    } else {
        Napi::TypeError::New(info.Env(), "Unxpected constructor parameters!").ThrowAsJavaScriptException();
    }
}

The problem is that Napi does not raise this exception to a JS level.
In the same time after the validation info.Env().GetAndClearPendingException() returns my error.

@mhdawson
Copy link
Member

@mhdawson mhdawson commented Aug 17, 2020

Which version of node-addon-api are you using. There were some fixes that went in in later versions to try to address some issues in this area.

@mhdawson
Copy link
Member

@mhdawson mhdawson commented Aug 17, 2020

Second question do you have C++ exceptions enabled or not?

@Ayzrian Ayzrian closed this Aug 18, 2020
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