Skip to content

Commit

Permalink
src: externalize remaining errors in src/*.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 11, 2016
1 parent a5d97de commit 9fc6ec1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
19 changes: 18 additions & 1 deletion src/messages/en/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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')
Expand Down
10 changes: 5 additions & 5 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,25 @@ 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
Local<Value> gid_v = js_options->Get(env->gid_string());
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 ?
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/string_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 10 additions & 12 deletions src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,13 @@ void TLSWrap::Wrap(const FunctionCallbackInfo<Value>& 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<External> stream_obj = args[0].As<External>();
Local<Object> sc = args[1].As<Object>();
Expand Down Expand Up @@ -217,7 +215,7 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap = Unwrap<TLSWrap>(args.Holder());

if (wrap->started_)
return env->ThrowError("Already started.");
return THROWI18NERROR(env, ALREADY_STARTED);
wrap->started_ = true;

// Send ClientHello handshake
Expand Down Expand Up @@ -726,10 +724,10 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap = Unwrap<TLSWrap>(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()) {
Expand Down Expand Up @@ -757,8 +755,8 @@ void TLSWrap::EnableSessionCallbacks(
const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap = Unwrap<TLSWrap>(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);
Expand Down Expand Up @@ -821,10 +819,10 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap = Unwrap<TLSWrap>(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;
Expand Down
2 changes: 1 addition & 1 deletion src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void ErrName(const FunctionCallbackInfo<Value>& 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));
}
Expand Down

0 comments on commit 9fc6ec1

Please sign in to comment.