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

src: move node_binding to modern THROW_ERR* #35469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,14 @@ An unknown cipher was specified.
An unknown Diffie-Hellman group name was given. See
[`crypto.getDiffieHellman()`][] for a list of valid group names.

<a id="ERR_DLOPEN_FAILED"></a>
### `ERR_DLOPEN_FAILED`
<!-- YAML
added: REPLACEME
-->

A call to `process.dlopen()` failed.

<a id="ERR_DIR_CLOSED"></a>
### `ERR_DIR_CLOSED`

Expand Down Expand Up @@ -1502,6 +1510,15 @@ An invalid HTTP token was supplied.

An IP address is not valid.

<a id="ERR_INVALID_MODULE"></a>
### `ERR_INVALID_MODULE`
<!-- YAML
added: REPLACEME
-->

An attempt was made to load a module that does not exist or was otherwise not
valid.

<a id="ERR_INVALID_MODULE_SPECIFIER"></a>
### `ERR_INVALID_MODULE_SPECIFIER`

Expand Down
38 changes: 18 additions & 20 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "node_native_module_env.h"
#include "util.h"

#include <string>

#if HAVE_OPENSSL
#define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap)
#else
Expand Down Expand Up @@ -424,13 +426,13 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
CHECK_NULL(thread_local_modpending);

if (args.Length() < 2) {
env->ThrowError("process.dlopen needs at least 2 arguments.");
return;
return THROW_ERR_MISSING_ARGS(
env, "process.dlopen needs at least 2 arguments");
}

int32_t flags = DLib::kDefaultFlags;
if (args.Length() > 2 && !args[2]->Int32Value(context).To(&flags)) {
return env->ThrowTypeError("flag argument must be an integer.");
return THROW_ERR_INVALID_ARG_TYPE(env, "flag argument must be an integer.");
}

Local<Object> module;
Expand All @@ -456,15 +458,13 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
thread_local_modpending = nullptr;

if (!is_opened) {
Local<String> errmsg =
OneByteString(env->isolate(), dlib->errmsg_.c_str());
std::string errmsg = dlib->errmsg_.c_str();
dlib->Close();
#ifdef _WIN32
// Windows needs to add the filename into the error message
errmsg = String::Concat(
env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked());
errmsg += *filename;
#endif // _WIN32
env->isolate()->ThrowException(Exception::Error(errmsg));
THROW_ERR_DLOPEN_FAILED(env, errmsg.c_str());
return false;
}

Expand Down Expand Up @@ -494,7 +494,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
sizeof(errmsg),
"Module did not self-register: '%s'.",
*filename);
env->ThrowError(errmsg);
THROW_ERR_DLOPEN_FAILED(env, errmsg);
return false;
}
}
Expand Down Expand Up @@ -525,7 +525,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
// NOTE: `mp` is allocated inside of the shared library's memory, calling
// `dlclose` will deallocate it
dlib->Close();
env->ThrowError(errmsg);
THROW_ERR_DLOPEN_FAILED(env, errmsg);
return false;
}
CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0);
Expand All @@ -538,7 +538,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
mp->nm_register_func(exports, module, mp->nm_priv);
} else {
dlib->Close();
env->ThrowError("Module has no declared entry point.");
THROW_ERR_DLOPEN_FAILED(env, "Module has no declared entry point.");
return false;
}

Expand Down Expand Up @@ -577,12 +577,6 @@ static Local<Object> InitModule(Environment* env,
return exports;
}

static void ThrowIfNoSuchModule(Environment* env, const char* module_v) {
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg), "No such module: %s", module_v);
env->ThrowError(errmsg);
}

void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -611,7 +605,9 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
env->isolate()))
.FromJust());
} else {
return ThrowIfNoSuchModule(env, *module_v);
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg), "No such module: %s", *module_v);
return THROW_ERR_INVALID_MODULE(env, errmsg);
}

args.GetReturnValue().Set(exports);
Expand Down Expand Up @@ -646,7 +642,7 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
sizeof(errmsg),
"No such module was linked: %s",
*module_name_v);
return env->ThrowError(errmsg);
return THROW_ERR_INVALID_MODULE(env, errmsg);
}

Local<Object> module = Object::New(env->isolate());
Expand All @@ -661,7 +657,9 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
} else if (mod->nm_register_func != nullptr) {
mod->nm_register_func(exports, module, mod->nm_priv);
} else {
return env->ThrowError("Linked module has no declared entry point.");
return THROW_ERR_INVALID_MODULE(
env,
"Linked moduled has no declared entry point.");
}

auto effective_exports =
Expand Down
4 changes: 4 additions & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ void OnFatalError(const char* location, const char* message);
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, Error) \
V(ERR_CRYPTO_JOB_INIT_FAILED, Error) \
V(ERR_DLOPEN_FAILED, Error) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I am missing something...these codes are not documented yet?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. At some point should likely include node_errors.h in the lint rule that checks those.

V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
V(ERR_INVALID_ARG_VALUE, TypeError) \
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
V(ERR_INVALID_ARG_TYPE, TypeError) \
V(ERR_INVALID_MODULE, Error) \
V(ERR_INVALID_THIS, TypeError) \
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
Expand Down Expand Up @@ -128,8 +130,10 @@ void OnFatalError(const char* location, const char* message);
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, "Unsupported crypto operation") \
V(ERR_CRYPTO_JOB_INIT_FAILED, "Failed to initialize crypto job config") \
V(ERR_DLOPEN_FAILED, "DLOpen failed") \
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \
"Context not associated with Node.js environment") \
V(ERR_INVALID_MODULE, "No such module") \
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
Expand Down