diff --git a/src/messages/en/messages.h b/src/messages/en/messages.h index 463db83e79bfb2..547f254bf8436b 100644 --- a/src/messages/en/messages.h +++ b/src/messages/en/messages.h @@ -8,8 +8,8 @@ // The messages used in src/*.cc // These are used only within the Node.js native source - #define STR_CONVERT_ARGS_TO_UTF8_FAIL "Could not convert arguments to utf8." +#define STR_OUTOFMEMORY "Out of memory" #define STR_CALLBACK_NOT_ASSIGNED \ "init callback is not assigned to a function" #define STR_HOOKSSHOULDNOTBESET \ @@ -213,6 +213,23 @@ #define STR_FAILED_TO_SET_DICTIONARY "Failed to set dictionary" #define STR_FAILED_TO_SET_PARAMETERS "Failed to set parameters" #define STR_FAILED_TO_RESET_STREAM "Failed to reset stream" +#define STR_UID_OUTOFRANGE "options.uid is out of range" +#define STR_GID_OUTOFRANGE "options.gid is out of range" +#define STR_OPTIONS_UID_NUMBER "options.uid should be a number" +#define STR_OPTIONS_GID_NUMBER "options.gid should be a number" +#define STR_BAD_INPUT_STRING "Bad input string" +#define STR_FIRST_ARGUMENT_STREAMWRAP \ + "First argument should be a StreamWrap instance" +#define STR_SECOND_ARGUMENT_SECURECONTEXT \ + "Second argument should be a SecureContext instance" +#define STR_THIRD_AGUMENT_BOOLEAN "Third argument should be boolean" +#define STR_ALREADY_STARTED "Already started." +#define STR_BAD_ARGUMENTS_TWO_BOOLEANS \ + "Bad arguments, expected two booleans" +#define STR_SETVERIFYMODE_AFTER_DESTROYSSL "SetVerifyMode after destroySSL" +#define STR_ENABLESESSIONCALLBACKS_AFTER_DESTROYSSL \ + "EnableSessionCallbacks after destroySSL" +#define STR_ERR_NOT_ZERO "err >= 0" // The messages used in lib/*.js // These are exposes as constants on require('internal/messages') diff --git a/src/process_wrap.cc b/src/process_wrap.cc index adf1606591f156..8696729b8c17b4 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -122,12 +122,12 @@ class ProcessWrap : public HandleWrap { if (uid_v->IsInt32()) { int32_t uid = uid_v->Int32Value(); if (uid & ~((uv_uid_t) ~0)) { - return env->ThrowRangeError("options.uid is out of range"); + return THROWI18NRANGEERROR(env, UID_OUTOFRANGE); } options.flags |= UV_PROCESS_SETUID; options.uid = (uv_uid_t) uid; } else if (!uid_v->IsUndefined() && !uid_v->IsNull()) { - return env->ThrowTypeError("options.uid should be a number"); + return THROWI18NTYPEERROR(env, OPTIONS_UID_NUMBER); } // options.gid @@ -135,12 +135,12 @@ class ProcessWrap : public HandleWrap { if (gid_v->IsInt32()) { int32_t gid = gid_v->Int32Value(); if (gid & ~((uv_gid_t) ~0)) { - return env->ThrowRangeError("options.gid is out of range"); + return THROWI18NRANGEERROR(env, GID_OUTOFRANGE); } options.flags |= UV_PROCESS_SETGID; options.gid = (uv_gid_t) gid; } else if (!gid_v->IsUndefined() && !gid_v->IsNull()) { - return env->ThrowTypeError("options.gid should be a number"); + return THROWI18NTYPEERROR(env, OPTIONS_GID_NUMBER); } // TODO(bnoordhuis) is this possible to do without mallocing ? @@ -152,7 +152,7 @@ class ProcessWrap : public HandleWrap { if (file.length() > 0) { options.file = *file; } else { - return env->ThrowTypeError("Bad argument"); + return THROWI18NTYPEERROR(env, BAD_ARGUMENT); } // options.args diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index e6236a6e529864..ea65773d47d789 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -160,7 +160,7 @@ void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) { if (buf->base == nullptr && size > 0) { FatalError( "node::StreamWrap::DoAlloc(size_t, uv_buf_t*, void*)", - "Out Of Memory"); + STR_OUTOFMEMORY); } } diff --git a/src/string_bytes.h b/src/string_bytes.h index 79520d24705ac0..f65d13a91cb70d 100644 --- a/src/string_bytes.h +++ b/src/string_bytes.h @@ -29,7 +29,7 @@ class StringBytes { enum encoding _default) { enum encoding enc = ParseEncoding(env->isolate(), encoding, _default); if (!StringBytes::IsValidString(env->isolate(), string, enc)) { - env->ThrowTypeError("Bad input string"); + THROWI18NTYPEERROR(env, BAD_INPUT_STRING); return false; } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index d7bf4ed8bee784..599673e99f05e4 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -164,15 +164,13 @@ void TLSWrap::Wrap(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1 || !args[0]->IsObject()) { - return env->ThrowTypeError( - "First argument should be a StreamWrap instance"); + return THROWI18NTYPEERROR(env, FIRST_ARGUMENT_STREAMWRAP); } if (args.Length() < 2 || !args[1]->IsObject()) { - return env->ThrowTypeError( - "Second argument should be a SecureContext instance"); + return THROWI18NTYPEERROR(env, SECOND_ARGUMENT_SECURECONTEXT); } if (args.Length() < 3 || !args[2]->IsBoolean()) - return env->ThrowTypeError("Third argument should be boolean"); + return THROWI18NTYPEERROR(env, THIRD_AGUMENT_BOOLEAN); Local stream_obj = args[0].As(); Local sc = args[1].As(); @@ -217,7 +215,7 @@ void TLSWrap::Start(const FunctionCallbackInfo& args) { TLSWrap* wrap = Unwrap(args.Holder()); if (wrap->started_) - return env->ThrowError("Already started."); + return THROWI18NERROR(env, ALREADY_STARTED); wrap->started_ = true; // Send ClientHello handshake @@ -726,10 +724,10 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo& args) { TLSWrap* wrap = Unwrap(args.Holder()); if (args.Length() < 2 || !args[0]->IsBoolean() || !args[1]->IsBoolean()) - return env->ThrowTypeError("Bad arguments, expected two booleans"); + return THROWI18NTYPEERROR(env, BAD_ARGUMENTS_TWO_BOOLEANS); if (wrap->ssl_ == nullptr) - return env->ThrowTypeError("SetVerifyMode after destroySSL"); + return THROWI18NTYPEERROR(env, SETVERIFYMODE_AFTER_DESTROYSSL); int verify_mode; if (wrap->is_server()) { @@ -757,8 +755,8 @@ void TLSWrap::EnableSessionCallbacks( const FunctionCallbackInfo& args) { TLSWrap* wrap = Unwrap(args.Holder()); if (wrap->ssl_ == nullptr) { - return wrap->env()->ThrowTypeError( - "EnableSessionCallbacks after destroySSL"); + return THROWI18NTYPEERROR(wrap->env(), + ENABLESESSIONCALLBACKS_AFTER_DESTROYSSL); } wrap->enable_session_callbacks(); NodeBIO::FromBIO(wrap->enc_in_)->set_initial(kMaxHelloLength); @@ -821,10 +819,10 @@ void TLSWrap::SetServername(const FunctionCallbackInfo& args) { TLSWrap* wrap = Unwrap(args.Holder()); if (args.Length() < 1 || !args[0]->IsString()) - return env->ThrowTypeError("First argument should be a string"); + return THROWI18NTYPEERROR(env, FIRST_ARGUMENT_STRING); if (wrap->started_) - return env->ThrowError("Already started."); + return THROWI18NERROR(env, ALREADY_STARTED); if (!wrap->is_client()) return; diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index cb678f14fb3826..5f39f4f38c6a22 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -347,7 +347,7 @@ void UDPWrap::OnAlloc(uv_handle_t* handle, if (buf->base == nullptr && suggested_size > 0) { FatalError("node::UDPWrap::OnAlloc(uv_handle_t*, size_t, uv_buf_t*)", - "Out Of Memory"); + STR_OUTOFMEMORY); } } diff --git a/src/uv.cc b/src/uv.cc index d03e7750083b4c..7b33a543f27428 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -20,7 +20,7 @@ void ErrName(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); int err = args[0]->Int32Value(); if (err >= 0) - return env->ThrowError("err >= 0"); + return THROWI18NERROR(env, ERR_NOT_ZERO); const char* name = uv_err_name(err); args.GetReturnValue().Set(OneByteString(env->isolate(), name)); }