Skip to content

Commit 2310f67

Browse files
committed
src: move node_binding to modern THROW_ERR*
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #35469 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent eb322bb commit 2310f67

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

doc/api/errors.md

+17
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,14 @@ An unknown cipher was specified.
960960
An unknown Diffie-Hellman group name was given. See
961961
[`crypto.getDiffieHellman()`][] for a list of valid group names.
962962

963+
<a id="ERR_DLOPEN_FAILED"></a>
964+
### `ERR_DLOPEN_FAILED`
965+
<!-- YAML
966+
added: REPLACEME
967+
-->
968+
969+
A call to `process.dlopen()` failed.
970+
963971
<a id="ERR_DIR_CLOSED"></a>
964972
### `ERR_DIR_CLOSED`
965973

@@ -1502,6 +1510,15 @@ An invalid HTTP token was supplied.
15021510

15031511
An IP address is not valid.
15041512

1513+
<a id="ERR_INVALID_MODULE"></a>
1514+
### `ERR_INVALID_MODULE`
1515+
<!-- YAML
1516+
added: REPLACEME
1517+
-->
1518+
1519+
An attempt was made to load a module that does not exist or was otherwise not
1520+
valid.
1521+
15051522
<a id="ERR_INVALID_MODULE_SPECIFIER"></a>
15061523
### `ERR_INVALID_MODULE_SPECIFIER`
15071524

src/node_binding.cc

+18-20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "node_native_module_env.h"
77
#include "util.h"
88

9+
#include <string>
10+
911
#if HAVE_OPENSSL
1012
#define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap)
1113
#else
@@ -424,13 +426,13 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
424426
CHECK_NULL(thread_local_modpending);
425427

426428
if (args.Length() < 2) {
427-
env->ThrowError("process.dlopen needs at least 2 arguments.");
428-
return;
429+
return THROW_ERR_MISSING_ARGS(
430+
env, "process.dlopen needs at least 2 arguments");
429431
}
430432

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

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

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

@@ -494,7 +494,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
494494
sizeof(errmsg),
495495
"Module did not self-register: '%s'.",
496496
*filename);
497-
env->ThrowError(errmsg);
497+
THROW_ERR_DLOPEN_FAILED(env, errmsg);
498498
return false;
499499
}
500500
}
@@ -525,7 +525,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
525525
// NOTE: `mp` is allocated inside of the shared library's memory, calling
526526
// `dlclose` will deallocate it
527527
dlib->Close();
528-
env->ThrowError(errmsg);
528+
THROW_ERR_DLOPEN_FAILED(env, errmsg);
529529
return false;
530530
}
531531
CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0);
@@ -538,7 +538,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
538538
mp->nm_register_func(exports, module, mp->nm_priv);
539539
} else {
540540
dlib->Close();
541-
env->ThrowError("Module has no declared entry point.");
541+
THROW_ERR_DLOPEN_FAILED(env, "Module has no declared entry point.");
542542
return false;
543543
}
544544

@@ -577,12 +577,6 @@ static Local<Object> InitModule(Environment* env,
577577
return exports;
578578
}
579579

580-
static void ThrowIfNoSuchModule(Environment* env, const char* module_v) {
581-
char errmsg[1024];
582-
snprintf(errmsg, sizeof(errmsg), "No such module: %s", module_v);
583-
env->ThrowError(errmsg);
584-
}
585-
586580
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
587581
Environment* env = Environment::GetCurrent(args);
588582

@@ -611,7 +605,9 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
611605
env->isolate()))
612606
.FromJust());
613607
} else {
614-
return ThrowIfNoSuchModule(env, *module_v);
608+
char errmsg[1024];
609+
snprintf(errmsg, sizeof(errmsg), "No such module: %s", *module_v);
610+
return THROW_ERR_INVALID_MODULE(env, errmsg);
615611
}
616612

617613
args.GetReturnValue().Set(exports);
@@ -646,7 +642,7 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
646642
sizeof(errmsg),
647643
"No such module was linked: %s",
648644
*module_name_v);
649-
return env->ThrowError(errmsg);
645+
return THROW_ERR_INVALID_MODULE(env, errmsg);
650646
}
651647

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

667665
auto effective_exports =

src/node_errors.h

+4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ void OnFatalError(const char* location, const char* message);
5353
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
5454
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, Error) \
5555
V(ERR_CRYPTO_JOB_INIT_FAILED, Error) \
56+
V(ERR_DLOPEN_FAILED, Error) \
5657
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
5758
V(ERR_INVALID_ARG_VALUE, TypeError) \
5859
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
5960
V(ERR_INVALID_ARG_TYPE, TypeError) \
61+
V(ERR_INVALID_MODULE, Error) \
6062
V(ERR_INVALID_THIS, TypeError) \
6163
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
6264
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
@@ -128,8 +130,10 @@ void OnFatalError(const char* location, const char* message);
128130
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
129131
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, "Unsupported crypto operation") \
130132
V(ERR_CRYPTO_JOB_INIT_FAILED, "Failed to initialize crypto job config") \
133+
V(ERR_DLOPEN_FAILED, "DLOpen failed") \
131134
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \
132135
"Context not associated with Node.js environment") \
136+
V(ERR_INVALID_MODULE, "No such module") \
133137
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
134138
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
135139
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \

0 commit comments

Comments
 (0)