Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Stack traces for mjsunit errors, better error reporting function.
Browse files Browse the repository at this point in the history
The error reporting function tries to look at the "stack" element of the
exception.
  • Loading branch information
ry committed Aug 31, 2009
1 parent 7beea2c commit afd9e71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,29 @@ ToCString(const v8::String::Utf8Value& value)
return *value ? *value : "<string conversion failed>";
}

void
ReportException(TryCatch* try_catch)
static void
ReportException (TryCatch &try_catch)
{
HandleScope handle_scope;
String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
Handle<Message> message = try_catch->Message();
Handle<Message> message = try_catch.Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
fprintf(stderr, "%s\n", exception_string);
} else {
fprintf(stderr, "Error: (no message)\n");
return;
}
Handle<Value> error = try_catch.Exception();
Handle<String> stack;
if (error->IsObject()) {
Handle<Object> obj = Handle<Object>::Cast(error);
Handle<Value> raw_stack = obj->Get(String::New("stack"));
if (raw_stack->IsString()) stack = Handle<String>::Cast(raw_stack);
}
if (stack.IsEmpty()) {
String::Utf8Value exception(error);

// Print (filename):(line number): (message).
String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, *exception);
// Print line of source code.
String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = ToCString(sourceline);
Expand All @@ -65,6 +71,11 @@ ReportException(TryCatch* try_catch)
fprintf(stderr, "\n");

message->PrintCurrentStackTrace(stderr);


} else {
String::Utf8Value trace(stack);
fprintf(stderr, "%s\n", *trace);
}
}

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

Handle<Script> script = Script::Compile(source, filename);
if (script.IsEmpty()) {
ReportException(&try_catch);
ReportException(try_catch);
exit(1);
}

Handle<Value> result = script->Run();
if (result.IsEmpty()) {
ReportException(&try_catch);
ReportException(try_catch);
exit(1);
}

Expand Down Expand Up @@ -162,7 +173,7 @@ OnFatalError (const char* location, const char* message)
void
node::FatalException (TryCatch &try_catch)
{
ReportException(&try_catch);
ReportException(try_catch);
exit(1);
}

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

ExecuteNativeJS("util.js", native_util);
ExecuteNativeJS("events.js", native_events);

Stdio::Initialize(node_obj);
Timer::Initialize(node_obj);
ChildProcess::Initialize(node_obj);
Expand All @@ -266,8 +280,6 @@ Load (int argc, char *argv[])
HTTPServer::Initialize(http);
HTTPConnection::Initialize(http);

ExecuteNativeJS("util.js", native_util);
ExecuteNativeJS("events.js", native_events);
ExecuteNativeJS("http.js", native_http);
ExecuteNativeJS("file.js", native_file);
ExecuteNativeJS("node.js", native_node);
Expand Down
1 change: 1 addition & 0 deletions test/mjsunit/mjsunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

function MjsUnitAssertionError(message) {
this.message = message;
Error.captureStackTrace(this, fail);
}

MjsUnitAssertionError.prototype.toString = function () {
Expand Down

0 comments on commit afd9e71

Please sign in to comment.