Skip to content

Commit e9b6b0b

Browse files
isaacsry
authored andcommitted
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.
1 parent d59512f commit e9b6b0b

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

lib/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ EventEmitter.prototype.emit = function (type) {
99
(isArray(this._events.error) && !this._events.error.length))
1010
{
1111
if (arguments[1] instanceof Error) {
12-
throw arguments[1];
12+
throw arguments[1]; // Unhandled 'error' event
1313
} else {
1414
throw new Error("Uncaught, unspecified 'error' event.");
1515
}

src/node.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,15 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
986986

987987
if (trace.length() > 0) {
988988
fprintf(stderr, "%s\n", *trace);
989+
} else {
990+
// this really only happens for RangeErrors, since they're the only
991+
// kind that won't have all this info in the trace.
992+
Local<Value> er = try_catch.Exception();
993+
String::Utf8Value msg(!er->IsObject() ? er->ToString()
994+
: er->ToObject()->Get(String::New("message"))->ToString());
995+
fprintf(stderr, "%s\n", *msg);
989996
}
997+
990998
fflush(stderr);
991999
}
9921000

src/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ process._tickCallback = function () {
4747
if (i+1 < l) {
4848
process._needTickCallback();
4949
}
50-
throw e;
50+
throw e; // process.nextTick error, or 'error' event on first tick
5151
}
5252

5353
nextTickQueue.splice(0, l);
@@ -99,7 +99,7 @@ var module = (function () {
9999
var m = new Module(id);
100100
internalModuleCache[id] = m;
101101
var e = m._compile(natives[id], id);
102-
if (e) throw e;
102+
if (e) throw e; // error compiling native module
103103
return m;
104104
}
105105

test/message/undefined_reference_in_new_context.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
before
22

33
node.js:*
4-
throw e;
4+
throw e; // process.nextTick error, or 'error' event on first tick
55
^
66
ReferenceError: foo is not defined
77
at evalmachine.<anonymous>:*

0 commit comments

Comments
 (0)