From d8b6fbf0e1767e5de6f083e9f85ffa7f8534782f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 2 Oct 2020 14:10:55 -0700 Subject: [PATCH] src: move node_binding to modern THROW_ERR* Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/35469 Reviewed-By: Joyee Cheung --- doc/api/errors.md | 17 +++++++++++++++++ src/node_binding.cc | 38 ++++++++++++++++++-------------------- src/node_errors.h | 4 ++++ 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 1c98ce88fb464b..9f12a9cba1a923 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -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. + +### `ERR_DLOPEN_FAILED` + + +A call to `process.dlopen()` failed. + ### `ERR_DIR_CLOSED` @@ -1502,6 +1510,15 @@ An invalid HTTP token was supplied. An IP address is not valid. + +### `ERR_INVALID_MODULE` + + +An attempt was made to load a module that does not exist or was otherwise not +valid. + ### `ERR_INVALID_MODULE_SPECIFIER` diff --git a/src/node_binding.cc b/src/node_binding.cc index 0d577a8c8e82eb..0db930adff1a9f 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -6,6 +6,8 @@ #include "node_native_module_env.h" #include "util.h" +#include + #if HAVE_OPENSSL #define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap) #else @@ -424,13 +426,13 @@ void DLOpen(const FunctionCallbackInfo& 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 module; @@ -456,15 +458,13 @@ void DLOpen(const FunctionCallbackInfo& args) { thread_local_modpending = nullptr; if (!is_opened) { - Local 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; } @@ -494,7 +494,7 @@ void DLOpen(const FunctionCallbackInfo& args) { sizeof(errmsg), "Module did not self-register: '%s'.", *filename); - env->ThrowError(errmsg); + THROW_ERR_DLOPEN_FAILED(env, errmsg); return false; } } @@ -525,7 +525,7 @@ void DLOpen(const FunctionCallbackInfo& 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); @@ -538,7 +538,7 @@ void DLOpen(const FunctionCallbackInfo& 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; } @@ -577,12 +577,6 @@ static Local 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& args) { Environment* env = Environment::GetCurrent(args); @@ -611,7 +605,9 @@ void GetInternalBinding(const FunctionCallbackInfo& 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); @@ -646,7 +642,7 @@ void GetLinkedBinding(const FunctionCallbackInfo& args) { sizeof(errmsg), "No such module was linked: %s", *module_name_v); - return env->ThrowError(errmsg); + return THROW_ERR_INVALID_MODULE(env, errmsg); } Local module = Object::New(env->isolate()); @@ -661,7 +657,9 @@ void GetLinkedBinding(const FunctionCallbackInfo& 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 = diff --git a/src/node_errors.h b/src/node_errors.h index 2d1d12f2d903bf..6158a968d27a9a 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -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) \ 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) \ @@ -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") \