Skip to content

Commit

Permalink
handle Malformed message fixed #9
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 15, 2013
1 parent 7baac97 commit f67ce53
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/client.js
Expand Up @@ -290,8 +290,6 @@ Client.prototype.createTable = function (meta, callback) {
var body = CreateTableRequest.serialize({
tableMeta: tableMeta
});
// console.log(tableMeta)
// console.log(body, CreateTableRequest.parse(body));
this.request('CreateTable', body, callback);
return this;
};
Expand Down Expand Up @@ -987,10 +985,19 @@ Client.prototype.request = function (optype, body, callback) {
}

if (statusCode !== 200) {
var e = ErrorMessage.parse(buf);
err = new Error(e.message);
err.name = e.code + 'Error';
err.code = e.code;
var info;
try {
info = ErrorMessage.parse(buf);
} catch (e) {
err = e;
err.name = 'OTSMalformedErrorMessageError';
err.data = buf && buf.toString();
}
if (info) {
err = new Error(info.message);
err.name = info.code + 'Error';
err.code = info.code;
}
err.serverId = serverId;
return callback(err, null, res);
}
Expand All @@ -1000,7 +1007,16 @@ Client.prototype.request = function (optype, body, callback) {
return callback(null, null, res);
}

var result = pbResponse.parse(buf);
var result = null;
try {
result = pbResponse.parse(buf);
} catch (e) {
err = e;
err.name = 'OTSMalformed' + optype + 'MessageError';
err.data = buf && buf.toString();
err.serverId = serverId;
}

callback(err, result, res);
});
});
Expand Down
29 changes: 29 additions & 0 deletions test/client.test.js
Expand Up @@ -10,6 +10,7 @@

var should = require('should');
var utility = require('utility');
var urllib = require('urllib');
var EventProxy = require('eventproxy').EventProxy;
var crypto = require('crypto');
var mm = require('mm');
Expand Down Expand Up @@ -101,6 +102,34 @@ describe('client.test.js', function() {
done();
});
});

it('should return ErrorMessage.parse Malformed message OTSMalformedErrorMessageError', function (done) {
mm.data(urllib, 'request', new Buffer('wrong format'));
client.listTableGroup(function (err, groups) {
should.exist(err);
err.name.should.equal('OTSMalformedErrorMessageError');
err.message.should.equal('Malformed message');
err.data.should.equal('wrong format');
should.not.exist(groups);
done();
});
});

it('should return pbResponse.parse Malformed message OTSMalformedListTableGroupMessageError', function (done) {
mm(urllib, 'request', function (url, options, callback) {
process.nextTick(function () {
callback(null, new Buffer('wrong response format'), {statusCode: 200});
});
});
client.listTableGroup(function (err, groups) {
should.exist(err);
err.name.should.equal('OTSMalformedListTableGroupMessageError');
err.message.should.equal('Malformed message');
err.data.should.equal('wrong response format');
should.not.exist(groups);
done();
});
});
});

describe('deleteTableGroup()', function () {
Expand Down

0 comments on commit f67ce53

Please sign in to comment.