Navigation Menu

Skip to content

Commit

Permalink
Suppress errors when callback functions is called twice or more
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 17, 2015
1 parent cc8a12e commit d10a86e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/droonga-protocol/connection.js
Expand Up @@ -245,15 +245,31 @@ Connection.prototype.emitMessage = function(type, body, callback, options) {
clearTimeout(timeoutId);
if (sendingMessages)
sendingMessages.callback = null;
callback(errorCode, response);
if (!callback)
return;
try {
callback(errorCode, response);
callback = null;
}
catch(error) {
this._logger.error(error);
}
}).bind(this));
options.timeout = toPositiveInteger(options.timeout) ||
DEFAULT_RESPONSE_TIMEOUT;
timeoutId = setTimeout((function() {
this.removeAllListeners(event);
if (sendingMessages)
sendingMessages.callback = null;
callback(ERROR_GATEWAY_TIMEOUT, null);
if (!callback)
return;
try {
callback(ERROR_GATEWAY_TIMEOUT, null);
callback = null;
}
catch(error) {
this._logger.error(error);
}
}).bind(this), options.timeout);
}
this._sendingMessages[id] = sendingMessages;
Expand Down Expand Up @@ -291,6 +307,7 @@ Connection.prototype.close = function() {
if (typeof callback == 'function') {
try {
callback(ERROR_SERVICE_UNAVAILABLE, null);
message.callback = callback = null;
}
catch(error) {
this._logger.error(error)
Expand Down

0 comments on commit d10a86e

Please sign in to comment.