events: provide better error message for unhandled error
authorEvan Lucas <evanlucas@me.com>
Thu, 7 May 2015 21:09:31 +0000 (16:09 -0500)
committerEvan Lucas <evanlucas@me.com>
Tue, 12 May 2015 13:33:20 +0000 (08:33 -0500)
Previously, in the event of an unhandled error event, if the error is a
not an actual Error, then a default error is thrown. Now, the argument
is appended to the error message and added as the `context` property
of the error.

PR-URL: https://github.com/iojs/io.js/pull/1654
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
lib/events.js
test/parallel/test-event-emitter-errors.js [new file with mode: 0644]

index 63d79ce..3ea798b 100644 (file)
@@ -140,7 +140,10 @@ EventEmitter.prototype.emit = function emit(type) {
     } else if (er instanceof Error) {
       throw er; // Unhandled 'error' event
     } else {
-      throw new Error('Uncaught, unspecified "error" event.');
+      // At least give some kind of context to the user
+      var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
+      err.context = er;
+      throw err;
     }
     return false;
   }
diff --git a/test/parallel/test-event-emitter-errors.js b/test/parallel/test-event-emitter-errors.js
new file mode 100644 (file)
index 0000000..5ee2c87
--- /dev/null
@@ -0,0 +1,8 @@
+var EventEmitter = require('events');
+var assert = require('assert');
+
+var EE = new EventEmitter();
+
+assert.throws(function() {
+  EE.emit('error', 'Accepts a string');
+}, /Accepts a string/);