Kouhei Sutou
null+****@clear*****
Mon Apr 7 16:00:43 JST 2014
Kouhei Sutou 2014-04-07 16:00:43 +0900 (Mon, 07 Apr 2014) New Revision: a4f348bdba7573295882a0cded6287cae43a4780 https://github.com/droonga/express-droonga/commit/a4f348bdba7573295882a0cded6287cae43a4780 Message: Fix a bug that server process is died on engine connection error "error" message is special in EventEmitter. If there is no handler for "error" message, node.js is aborted. This change adds "error" message handler. It returns an error response to clients. Modified files: lib/droonga-protocol/connection.js Modified: lib/droonga-protocol/connection.js (+28 -1) =================================================================== --- lib/droonga-protocol/connection.js 2014-04-07 13:10:13 +0900 (87bdf52) +++ lib/droonga-protocol/connection.js 2014-04-07 16:00:43 +0900 (385d84e) @@ -60,6 +60,7 @@ Connection.prototype._init = function() { }; Connection.prototype._initSender = function(wait) { + this._sendingMessages = {}; var options = { host: process.env.DROONGA_ENGINE_HOST || this._params.hostName || DEFAULT_FLUENT_HOST_NAME, @@ -69,7 +70,29 @@ Connection.prototype._initSender = function(wait) { var sender = fluent.createFluentSender(this.tag, options); this._sender = sender; this._sender.on('error', (function(error) { - this.emit('error', error); + var errorMessage = + 'An error is occurred in protocol adapter: ' + + '[' + error.name + '] ' + error.message; + var ids = Object.keys(this._sendingMessages); + if (ids.length == 0) { + console.error(errorMessage, error); + } else { + ids.forEach(function(id) { + var sendingMessage = this._sendingMessages[id]; + var message = { + inReplyTo: id, + statusCode: 500, + type: sendingMessage.type + '.result', + body: { + name: 'ProtocolAdapterError', + message: errorMessage, + detail: error + } + } + this.emit('reply:' + id, message.statusCode, message); + }.bind(this)); + this._sendingMessages = {}; + } }).bind(this)); }; @@ -109,6 +132,7 @@ Connection.prototype._handleMessage = function(envelope) { } var inReplyTo = envelope.inReplyTo; if (inReplyTo) { + delete this._sendingMessages[inReplyTo]; debug('Connection._handleMessage.reply %d:', this._id, inReplyTo); var errorCode = envelope.statusCode; if (!errorCode || isSuccess(errorCode)) @@ -199,6 +223,9 @@ Connection.prototype.emitMessage = function(type, body, callback, options) { } }).bind(this), options.timeout); } + this._sendingMessages[id] = { + type: type + }; this._sender.emit('message', envelope, options.emittedCallback); return envelope; }; -------------- next part -------------- HTML����������������������������...Download