Skip to content

Instantly share code, notes, and snippets.

@piscisaureus
Created February 14, 2011 15:14
Show Gist options
  • Save piscisaureus/826013 to your computer and use it in GitHub Desktop.
Save piscisaureus/826013 to your computer and use it in GitHub Desktop.
From 6bf7d33d9c87670c9455ebfe9187351a4f97926e Mon Sep 17 00:00:00 2001
From: Bert Belder <bertbelder@gmail.com>
Date: Mon, 14 Feb 2011 16:11:46 +0100
Subject: [PATCH 1/1] Trycatch in emit
---
lib/events.js | 5 ++++-
1 files changed, 4 insertions(+), 1 deletions(-)
diff --git a/lib/events.js b/lib/events.js
index 6505606..26a4f07 100644
--- a/lib/events.js
+++ b/lib/events.js
@@ -18,64 +18,67 @@ EventEmitter.prototype.emit = function(type) {
if (type === 'error') {
if (!this._events || !this._events.error ||
(isArray(this._events.error) && !this._events.error.length))
{
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
throw new Error("Uncaught, unspecified 'error' event.");
}
return false;
}
}
if (!this._events) return false;
var handler = this._events[type];
if (!handler) return false;
-
+try {
if (typeof handler == 'function') {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
var args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
return true;
} else if (isArray(handler)) {
var args = Array.prototype.slice.call(arguments, 1);
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
} else {
return false;
}
+} catch (e) {
+ // do some stuff
+}
};
// EventEmitter is defined in src/node_events.cc
// EventEmitter.prototype.emit() is also defined there.
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, listener);
if (!this._events[type]) {
--
1.7.3.1.msysgit.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment