Skip to content

Commit

Permalink
Cleanup errors. Closes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 11, 2015
1 parent 7fb1e01 commit 5c11bfd
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 126 deletions.
87 changes: 44 additions & 43 deletions lib/client.js
Expand Up @@ -202,7 +202,7 @@
payload: options.payload
};

return this._send(request, callback);
return this._send(request, true, callback);
};

Client.prototype.message = function (message, callback) {
Expand All @@ -212,10 +212,10 @@
message: message
};

return this._send(request, callback);
return this._send(request, true, callback);
};

Client.prototype._send = function (request, callback) {
Client.prototype._send = function (request, track, callback) {

var self = this;

Expand All @@ -228,19 +228,25 @@
}

request.id = ++self._ids;

stringify(request, function (err, encoded) {

if (err) {
return callback(err);
}

self._requests[request.id] = callback;
if (track) {
self._requests[request.id] = callback;
}

try {
self._ws.send(encoded);
}
catch (err) {
delete self._requests[request.id];
if (track) {
delete self._requests[request.id];
}

return callback(err);
}
});
Expand All @@ -261,7 +267,7 @@
request.subs = subs;
}

return this._send(request, callback);
return this._send(request, true, callback);
};

Client.prototype.subscriptions = function () {
Expand Down Expand Up @@ -301,7 +307,7 @@
path: path
};

return this._send(request, function (err) {
return this._send(request, false, function (err) {

return handler(err); // Only called if send failed to transmit
});
Expand Down Expand Up @@ -352,7 +358,7 @@
path: path
};

return this._send(request); // Ignoring errors as the subscription handlers are already removed
return this._send(request, false); // Ignoring errors as the subscription handlers are already removed
};

Client.prototype._onMessage = function (message) {
Expand All @@ -365,6 +371,19 @@
return self.onError(err);
}

// Recreate error

var error = null;
if (update.statusCode &&
update.statusCode >= 400 &&
update.statusCode <= 599) {

error = new Error(update.payload.message || update.payload.error);
error.statusCode = update.statusCode;
error.data = update.payload;
error.headers = update.headers;
}

// Broadcast

if (update.type === 'broadcast') {
Expand All @@ -374,14 +393,13 @@
// Publish

if (update.type === 'pub') {
var handlers = self._subscriptions[update.path];
if (handlers) {
for (var i = 0, il = handlers.length; i < il; ++i) {
handlers[i](null, update.message);
}
}
return self._notifyHandlers(update.path, null, update.message);
}

return;
// Subscriptions

if (update.type === 'sub') {
return self._notifyHandlers(update.path, error);
}

// Lookup callback (message must include an id from this point)
Expand All @@ -392,55 +410,38 @@
return self.onError(new Error('Received response for missing request'));
}

// Subscriptions

if (update.type === 'sub') {
if (update.error) {
self._processSubscribeErrors(update);
}

return;
}

// Response

if (update.type === 'response') {
var error = (update.statusCode >= 400 && update.statusCode <= 599 ? new Error(update.payload.message) : null);
if (update.type === 'request') {
return callback(error, update.payload, update.statusCode, update.headers);
}

// Custom message

if (update.type === 'message') {
return callback(update.error ? new Error(update.error) : null, update.message);
return callback(error, update.message);
}

// Authentication

if (update.type === 'hello') {
if (update.subs) {
self._processSubscribeErrors(update.subs);
}

return callback(update.error ? new Error(update.error) : null);
return callback(error);
}

return self.onError(new Error('Received unknown response type: ' + update.type));
});
};

Client.prototype._processSubscribeErrors = function (updates) {
Client.prototype._notifyHandlers = function (path, err, message) {

updates = [].concat(updates);
for (var u = 0, ul = updates.length; u < ul; ++u) {
var update = updates[u];
var handlers = this._subscriptions[update.path];
if (handlers) {
delete this._subscriptions[update.path]; // Error means no longer subscribed
var handlers = this._subscriptions[path];
if (handlers) {
if (err) {
delete this._subscriptions[path]; // Error means no longer subscribed
}

for (var i = 0, il = handlers.length; i < il; ++i) {
handlers[i](new Error(update.error));
}
for (var i = 0, il = handlers.length; i < il; ++i) {
handlers[i](err, message);
}
}
};
Expand Down

0 comments on commit 5c11bfd

Please sign in to comment.