Skip to content

Commit afd9e71

Browse files
committed
Stack traces for mjsunit errors, better error reporting function.
The error reporting function tries to look at the "stack" element of the exception.
1 parent 7beea2c commit afd9e71

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/node.cc

+29-17
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,29 @@ ToCString(const v8::String::Utf8Value& value)
3232
return *value ? *value : "<string conversion failed>";
3333
}
3434

35-
void
36-
ReportException(TryCatch* try_catch)
35+
static void
36+
ReportException (TryCatch &try_catch)
3737
{
38-
HandleScope handle_scope;
39-
String::Utf8Value exception(try_catch->Exception());
40-
const char* exception_string = ToCString(exception);
41-
Handle<Message> message = try_catch->Message();
38+
Handle<Message> message = try_catch.Message();
4239
if (message.IsEmpty()) {
43-
// V8 didn't provide any extra information about this error; just
44-
// print the exception.
45-
fprintf(stderr, "%s\n", exception_string);
46-
} else {
40+
fprintf(stderr, "Error: (no message)\n");
41+
return;
42+
}
43+
Handle<Value> error = try_catch.Exception();
44+
Handle<String> stack;
45+
if (error->IsObject()) {
46+
Handle<Object> obj = Handle<Object>::Cast(error);
47+
Handle<Value> raw_stack = obj->Get(String::New("stack"));
48+
if (raw_stack->IsString()) stack = Handle<String>::Cast(raw_stack);
49+
}
50+
if (stack.IsEmpty()) {
51+
String::Utf8Value exception(error);
52+
4753
// Print (filename):(line number): (message).
4854
String::Utf8Value filename(message->GetScriptResourceName());
4955
const char* filename_string = ToCString(filename);
5056
int linenum = message->GetLineNumber();
51-
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
57+
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, *exception);
5258
// Print line of source code.
5359
String::Utf8Value sourceline(message->GetSourceLine());
5460
const char* sourceline_string = ToCString(sourceline);
@@ -65,6 +71,11 @@ ReportException(TryCatch* try_catch)
6571
fprintf(stderr, "\n");
6672

6773
message->PrintCurrentStackTrace(stderr);
74+
75+
76+
} else {
77+
String::Utf8Value trace(stack);
78+
fprintf(stderr, "%s\n", *trace);
6879
}
6980
}
7081

@@ -78,13 +89,13 @@ ExecuteString(v8::Handle<v8::String> source,
7889

7990
Handle<Script> script = Script::Compile(source, filename);
8091
if (script.IsEmpty()) {
81-
ReportException(&try_catch);
92+
ReportException(try_catch);
8293
exit(1);
8394
}
8495

8596
Handle<Value> result = script->Run();
8697
if (result.IsEmpty()) {
87-
ReportException(&try_catch);
98+
ReportException(try_catch);
8899
exit(1);
89100
}
90101

@@ -162,7 +173,7 @@ OnFatalError (const char* location, const char* message)
162173
void
163174
node::FatalException (TryCatch &try_catch)
164175
{
165-
ReportException(&try_catch);
176+
ReportException(try_catch);
166177
exit(1);
167178
}
168179

@@ -210,7 +221,7 @@ ExecuteNativeJS (const char *filename, const char *data)
210221
if (try_catch.HasCaught()) {
211222
puts("There is an error in Node's built-in javascript");
212223
puts("This should be reported as a bug!");
213-
ReportException(&try_catch);
224+
ReportException(try_catch);
214225
exit(1);
215226
}
216227
}
@@ -242,6 +253,9 @@ Load (int argc, char *argv[])
242253
EventEmitter::constructor_template->GetFunction());
243254
Promise::Initialize(node_obj);
244255

256+
ExecuteNativeJS("util.js", native_util);
257+
ExecuteNativeJS("events.js", native_events);
258+
245259
Stdio::Initialize(node_obj);
246260
Timer::Initialize(node_obj);
247261
ChildProcess::Initialize(node_obj);
@@ -266,8 +280,6 @@ Load (int argc, char *argv[])
266280
HTTPServer::Initialize(http);
267281
HTTPConnection::Initialize(http);
268282

269-
ExecuteNativeJS("util.js", native_util);
270-
ExecuteNativeJS("events.js", native_events);
271283
ExecuteNativeJS("http.js", native_http);
272284
ExecuteNativeJS("file.js", native_file);
273285
ExecuteNativeJS("node.js", native_node);

test/mjsunit/mjsunit.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
function MjsUnitAssertionError(message) {
3535
this.message = message;
36+
Error.captureStackTrace(this, fail);
3637
}
3738

3839
MjsUnitAssertionError.prototype.toString = function () {

0 commit comments

Comments
 (0)