Skip to content

Commit

Permalink
connection: fix error handling in optional cbs
Browse files Browse the repository at this point in the history
This commit makes error handling in methods that take optional
callbacks more robust.

* Fix a possible TypeError in `inspectInterface()`.
* Ensure that an error is not lost in `inspectInterface()` and
  `callMethod()` by emitting it on the connection if there's no
  callback.

PR-URL: #147
Reviewed-By: Mykola Bilochub <[email protected]>
  • Loading branch information
aqrln committed May 12, 2017
1 parent cf4e5c4 commit e443548
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ Connection.prototype.callMethod = function(
) {
const packet = this.createPacket('call', interfaceName, methodName, args);
const packetId = packet.call[0];
this._callbacks[packetId] = callback || common.doNothing;
this._callbacks[packetId] = callback || ((error) => {
if (error) this.emit('error', error);
});
this._send(packet);
};

Expand Down Expand Up @@ -160,7 +162,12 @@ Connection.prototype.inspectInterface = function(interfaceName, callback) {

this._callbacks[packetId] = (error, ...methods) => {
if (error) {
return callback(error);
if (callback) {
callback(error);
} else {
this.emit('error', error);
}
return;
}

const proxy = new RemoteProxy(this, interfaceName, methods);
Expand Down

0 comments on commit e443548

Please sign in to comment.