From 38c5460443f9eec6b1ec3f70cdb85eaf5ada73d5 Mon Sep 17 00:00:00 2001 From: Dan Maglasang Date: Thu, 25 Jan 2018 03:49:35 -0500 Subject: [PATCH] Fix issue with connection failures silently failing --- lib/nodejs/lib/thrift/http_connection.js | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/nodejs/lib/thrift/http_connection.js b/lib/nodejs/lib/thrift/http_connection.js index f3fcd741137..82e92839099 100644 --- a/lib/nodejs/lib/thrift/http_connection.js +++ b/lib/nodejs/lib/thrift/http_connection.js @@ -198,6 +198,31 @@ var HttpConnection = exports.HttpConnection = function(host, port, options) { self.transport.receiver(decodeCallback)(buf); }); }; + + function decodeConnectionErrorCallback(transport_with_data) { + // grab metadata from initial call + // eg. function name (fname) and seqId + var proto = new self.protocol(transport_with_data); + var header = proto.readMessageBegin(); + proto.readMessageEnd(); + + // set up the output listener + var output = new self.protocol(new self.transport(undefined, function(buf) { + self.transport.receiver(decodeCallback)(buf); + })); + + // write to the output with a TApplicationException + var connectionErrorMsg = 'Connection error: ' + this.host + ':' + this.port; + var x = new thrift.TApplicationException(thrift.TApplicationExceptionType.PROTOCOL_ERROR, connectionErrorMsg); + output.writeMessageBegin(header.fname, thrift.MessageType.EXCEPTION, header.rseqid); + x.write(output); + output.writeMessageEnd(); + output.flush(); + } + + this.respondWithConnectionError = function(data, err) { + self.transport.receiver(decodeConnectionErrorCallback.bind(this))(data); + }; }; util.inherits(HttpConnection, EventEmitter); @@ -215,6 +240,7 @@ HttpConnection.prototype.write = function(data) { https.request(self.nodeOptions, self.responseCallback) : http.request(self.nodeOptions, self.responseCallback); req.on('error', function(err) { + self.respondWithConnectionError(data, err); self.emit("error", err); }); req.write(data);