Skip to content
Browse files

Added some more error handling

Also fixes a bug with the error handling at the bottom of sendNotification.
  • Loading branch information...
1 parent 9595f96 commit 4911bb724cb9cd675ce48ce45dbaae95b6480e79 @argon committed Jul 5, 2012
Showing with 19 additions and 4 deletions.
  1. +19 −4 lib/connection.js
View
23 lib/connection.js
@@ -152,10 +152,11 @@ Connection.prototype.connect = function () {
debug("Connection established");
this.deferredConnection.resolve();
}.bind(this));
-
+
this.socket.on('data', this.handleTransmissionError.bind(this));
this.socket.on("drain", this.socketDrained.bind(this));
- this.socket.on("error", this.destroyConnection.bind(this));
+ this.socket.on('clientError', this.errorOccurred.bind(this));
+ this.socket.on("error", this.errorOccurred.bind(this));
this.socket.on("end", this.restartConnection.bind(this));
this.socket.once('close', this.restartConnection.bind(this));
}.bind(this)).fail(function (error) {
@@ -170,6 +171,20 @@ Connection.prototype.connect = function () {
/**
* @private
*/
+Connection.prototype.errorOccurred = function(err) {
+ debug("Socket error occurred", err);
+ if(!this.deferredConnection.promise.isResolved()) {
+ this.deferredConnection.reject(err);
+ }
+ else {
+ this.raiseError(err, null);
+ }
+ this.destroyConnection();
+};
+
+/**
+ * @private
+ */
Connection.prototype.socketDrained = function() {
debug("Socket drained");
if (this.socket && (this.socket.socket.bufferSize != 0 || !this.socket.writable)) {
@@ -289,7 +304,7 @@ Connection.prototype.handleTransmissionError = function (data) {
* @private
*/
Connection.prototype.raiseError = function(errorCode, notification) {
- debug("Raising error: %d", errorCode, notification);
+ debug("Raising error:", errorCode, notification);
if (typeof this.options.errorCallback == 'function') {
this.options.errorCallback(errorCode, notification);
}
@@ -368,7 +383,7 @@ Connection.prototype.sendNotification = function (notification) {
}
}.bind(this)).fail(function (error) {
this.raiseError(error, notification);
- });
+ }.bind(this));
};
module.exports = Connection;

0 comments on commit 4911bb7

Please sign in to comment.
Something went wrong with that request. Please try again.