Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed a failbug when combining chunks.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
commit 46dfb2a7f1f90926449bf3e1ed4e2545aab11070 1 parent 222cd81
@ncb000gt authored
Showing with 18 additions and 4 deletions.
  1. +18 −4 lib/elasticsearch.js
View
22 lib/elasticsearch.js
@@ -47,6 +47,11 @@ ElasticSearch.prototype.status = function(opts, cb) {
}
ElasticSearch.prototype.add = function(opts, doc, cb) {
+ if (opts && doc && !cb && typeof (doc) == 'function') {
+ cb = doc;
+ doc = opts;
+ opts = {};
+ }
var def = {
host: this.config.host,
port: this.config.port,
@@ -95,6 +100,7 @@ ElasticSearch.prototype._request = function(opts, data, cb) {
data = undefined;
}
var req = http.request(opts, function(res) {
+ var statusCode = res.statusCode;
var chunks = [],
length = 0;
res.on('data', function(chunk) {
@@ -107,14 +113,22 @@ ElasticSearch.prototype._request = function(opts, data, cb) {
total = 0;
chunks.forEach(function chunk_cb(chunk) {
chunk.copy(buf, total, 0);
- total += buf.length;
+ total += chunk.length;
});
- var res_json = JSON.parse(buf.toString());
+
+ //TODO: figure out a better way to do this, not sure it's possible with inconsistent messages from ES...
+ var res_json = {};
+ try {
+ res_json = JSON.parse(buf.toString());
+ } catch(e) {
+ res_json = {status: statusCode, error: buf.toString()};
+ }
if (cb) {
- if (res_json.status !== 200) {
+ //Success based status codes.
+ if (!([200, 201, 202, 205, 206].indexOf(statusCode))) {
cb(res_json);
} else {
- cb(undefined, res_json);
+ cb(null, res_json);
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.