summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2015-12-15 03:55:35 -0500
committerBrian White <mscdex@mscdex.net>2015-12-19 14:15:38 -0500
commit18490d3d5af3131751791e501302bc57865337b3 (patch)
tree78cb704b8126394b9b632484ca7bde17a06914e3 /src
parent4b0b991bf5bbb41e204a7b98f62a8afd00faf855 (diff)
downloadnode-new-18490d3d5af3131751791e501302bc57865337b3.tar.gz
module: always decorate thrown errors
This provides more information when encountering a syntax or similar error when executing a file with require(). Fixes: https://github.com/nodejs/node/issues/4286 PR-URL: https://github.com/nodejs/node/pull/4287 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/env.h3
-rw-r--r--src/node.cc14
-rw-r--r--src/node_util.cc16
3 files changed, 30 insertions, 3 deletions
diff --git a/src/env.h b/src/env.h
index 6151c57069..743bf057e8 100644
--- a/src/env.h
+++ b/src/env.h
@@ -51,7 +51,7 @@ namespace node {
V(alpn_buffer_string, "alpnBuffer") \
V(args_string, "args") \
V(argv_string, "argv") \
- V(arrow_message_string, "arrowMessage") \
+ V(arrow_message_string, "node:arrowMessage") \
V(async, "async") \
V(async_queue_string, "_asyncQueue") \
V(atime_string, "atime") \
@@ -71,6 +71,7 @@ namespace node {
V(cwd_string, "cwd") \
V(debug_port_string, "debugPort") \
V(debug_string, "debug") \
+ V(decorated_string, "node:decorated") \
V(dest_string, "dest") \
V(detached_string, "detached") \
V(dev_string, "dev") \
diff --git a/src/node.cc b/src/node.cc
index f9797e664e..894c8f7641 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -1399,6 +1399,15 @@ ssize_t DecodeWrite(Isolate* isolate,
return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
}
+bool IsExceptionDecorated(Environment* env, Local<Value> er) {
+ if (!er.IsEmpty() && er->IsObject()) {
+ Local<Object> err_obj = er.As<Object>();
+ Local<Value> decorated = err_obj->GetHiddenValue(env->decorated_string());
+ return !decorated.IsEmpty() && decorated->IsTrue();
+ }
+ return false;
+}
+
void AppendExceptionLine(Environment* env,
Local<Value> er,
Local<Message> message) {
@@ -1508,6 +1517,7 @@ static void ReportException(Environment* env,
Local<Value> trace_value;
Local<Value> arrow;
+ const bool decorated = IsExceptionDecorated(env, er);
if (er->IsUndefined() || er->IsNull()) {
trace_value = Undefined(env->isolate());
@@ -1522,7 +1532,7 @@ static void ReportException(Environment* env,
// range errors have a trace member set to undefined
if (trace.length() > 0 && !trace_value->IsUndefined()) {
- if (arrow.IsEmpty() || !arrow->IsString()) {
+ if (arrow.IsEmpty() || !arrow->IsString() || decorated) {
PrintErrorString("%s\n", *trace);
} else {
node::Utf8Value arrow_string(env->isolate(), arrow);
@@ -1554,7 +1564,7 @@ static void ReportException(Environment* env,
node::Utf8Value name_string(env->isolate(), name);
node::Utf8Value message_string(env->isolate(), message);
- if (arrow.IsEmpty() || !arrow->IsString()) {
+ if (arrow.IsEmpty() || !arrow->IsString() || decorated) {
PrintErrorString("%s: %s\n", *name_string, *message_string);
} else {
node::Utf8Value arrow_string(env->isolate(), arrow);
diff --git a/src/node_util.cc b/src/node_util.cc
index 1e0f214ae4..8475468c1f 100644
--- a/src/node_util.cc
+++ b/src/node_util.cc
@@ -52,6 +52,21 @@ static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(obj->GetHiddenValue(name));
}
+static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
+ Environment* env = Environment::GetCurrent(args);
+
+ if (!args[0]->IsObject())
+ return env->ThrowTypeError("obj must be an object");
+
+ if (!args[1]->IsString())
+ return env->ThrowTypeError("name must be a string");
+
+ Local<Object> obj = args[0].As<Object>();
+ Local<String> name = args[1].As<String>();
+
+ args.GetReturnValue().Set(obj->SetHiddenValue(name, args[2]));
+}
+
void Initialize(Local<Object> target,
Local<Value> unused,
@@ -63,6 +78,7 @@ void Initialize(Local<Object> target,
#undef V
env->SetMethod(target, "getHiddenValue", GetHiddenValue);
+ env->SetMethod(target, "setHiddenValue", SetHiddenValue);
}
} // namespace util