Skip to content

Commit

Permalink
src: move node_binding to modern THROW_ERR*
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #35469
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
jasnell authored and MylesBorins committed Aug 31, 2021
1 parent 82363d8 commit 4091eb9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
17 changes: 17 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,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_DEBUGGER_ERROR"></a>
### `ERR_DEBUGGER_ERROR`
<!-- YAML
Expand Down Expand Up @@ -1421,6 +1429,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 @@ -5,6 +5,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 @@ -416,13 +418,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 @@ -448,15 +450,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 @@ -486,7 +486,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 @@ -517,7 +517,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 @@ -530,7 +530,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 @@ -569,12 +569,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 @@ -603,7 +597,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 @@ -638,7 +634,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 @@ -653,7 +649,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 @@ -38,11 +38,13 @@ void OnFatalError(const char* location, const char* message);
V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, RangeError) \
V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
V(ERR_DLOPEN_FAILED, Error) \
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
V(ERR_INVALID_ADDRESS, 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 @@ -107,9 +109,11 @@ ERRORS_WITH_CODE(V)
"Input buffers must have the same byte length") \
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
V(ERR_DLOPEN_FAILED, "DLOpen failed") \
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \
"Context not associated with Node.js environment") \
V(ERR_INVALID_ADDRESS, "Invalid socket address") \
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

0 comments on commit 4091eb9

Please sign in to comment.