From e9b6b0b327e6f7beff05ef9b3c2fe1b166ad0a34 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 1 Oct 2010 23:22:34 -0700 Subject: [PATCH] Report "weird" errors a little better. There are a few kinds of errors that are very confusing. 1. Errors raised in nextTick 2. Errors emitted on the "error" event 3. RangeErrors that crash the program (or anything without a stack trace) Long traces will make make these better, of course. In the meantime, this adds a few handy signposts (in the form of better error reporting and comments on the otherwise inscrutable code printed to the terminal) that can help new users find the cause, or at least, ask for help more effectively. --- lib/events.js | 2 +- src/node.cc | 8 ++++++++ src/node.js | 4 ++-- test/message/undefined_reference_in_new_context.out | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/events.js b/lib/events.js index d41608deee..ea8f7aa628 100644 --- a/lib/events.js +++ b/lib/events.js @@ -9,7 +9,7 @@ EventEmitter.prototype.emit = function (type) { (isArray(this._events.error) && !this._events.error.length)) { if (arguments[1] instanceof Error) { - throw arguments[1]; + throw arguments[1]; // Unhandled 'error' event } else { throw new Error("Uncaught, unspecified 'error' event."); } diff --git a/src/node.cc b/src/node.cc index 8f13c64b64..004fa99b03 100644 --- a/src/node.cc +++ b/src/node.cc @@ -986,7 +986,15 @@ static void ReportException(TryCatch &try_catch, bool show_line) { if (trace.length() > 0) { fprintf(stderr, "%s\n", *trace); + } else { + // this really only happens for RangeErrors, since they're the only + // kind that won't have all this info in the trace. + Local er = try_catch.Exception(); + String::Utf8Value msg(!er->IsObject() ? er->ToString() + : er->ToObject()->Get(String::New("message"))->ToString()); + fprintf(stderr, "%s\n", *msg); } + fflush(stderr); } diff --git a/src/node.js b/src/node.js index bdc208455d..7d177eb6ce 100644 --- a/src/node.js +++ b/src/node.js @@ -47,7 +47,7 @@ process._tickCallback = function () { if (i+1 < l) { process._needTickCallback(); } - throw e; + throw e; // process.nextTick error, or 'error' event on first tick } nextTickQueue.splice(0, l); @@ -99,7 +99,7 @@ var module = (function () { var m = new Module(id); internalModuleCache[id] = m; var e = m._compile(natives[id], id); - if (e) throw e; + if (e) throw e; // error compiling native module return m; } diff --git a/test/message/undefined_reference_in_new_context.out b/test/message/undefined_reference_in_new_context.out index e9a5675828..6d3ab75fff 100644 --- a/test/message/undefined_reference_in_new_context.out +++ b/test/message/undefined_reference_in_new_context.out @@ -1,7 +1,7 @@ before node.js:* - throw e; + throw e; // process.nextTick error, or 'error' event on first tick ^ ReferenceError: foo is not defined at evalmachine.:*