Fixed server to handle invalid arguments without breaking

pull/1655/head
murgatroid99 10 years ago
parent 778c61b6fc
commit 04589a7e0c
  1. 29
      src/node/src/server.js
  2. 11
      src/node/test/surface_test.js

@ -291,7 +291,15 @@ function _read(size) {
return; return;
} }
var data = event.read; var data = event.read;
if (self.push(self.deserialize(data)) && data !== null) { var deserialized;
try {
deserialized = self.deserialize(data);
} catch (e) {
e.code = grpc.status.INVALID_ARGUMENT;
self.emit('error', e);
return;
}
if (self.push(deserialized) && data !== null) {
var read_batch = {}; var read_batch = {};
read_batch[grpc.opType.RECV_MESSAGE] = true; read_batch[grpc.opType.RECV_MESSAGE] = true;
self.call.startBatch(read_batch, readCallback); self.call.startBatch(read_batch, readCallback);
@ -354,7 +362,13 @@ function handleUnary(call, handler, metadata) {
handleError(call, err); handleError(call, err);
return; return;
} }
emitter.request = handler.deserialize(result.read); try {
emitter.request = handler.deserialize(result.read);
} catch (e) {
e.code = grpc.status.INVALID_ARGUMENT;
handleError(call, e);
return;
}
if (emitter.cancelled) { if (emitter.cancelled) {
return; return;
} }
@ -388,7 +402,13 @@ function handleServerStreaming(call, handler, metadata) {
stream.emit('error', err); stream.emit('error', err);
return; return;
} }
stream.request = handler.deserialize(result.read); try {
stream.request = handler.deserialize(result.read);
} catch (e) {
e.code = grpc.status.INVALID_ARGUMENT;
stream.emit('error', e);
return;
}
handler.func(stream); handler.func(stream);
}); });
} }
@ -401,6 +421,9 @@ function handleServerStreaming(call, handler, metadata) {
*/ */
function handleClientStreaming(call, handler, metadata) { function handleClientStreaming(call, handler, metadata) {
var stream = new ServerReadableStream(call, handler.deserialize); var stream = new ServerReadableStream(call, handler.deserialize);
stream.on('error', function(error) {
handleError(call, error);
});
waitForCancel(call, stream); waitForCancel(call, stream);
var metadata_batch = {}; var metadata_batch = {};
metadata_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata; metadata_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata;

@ -192,7 +192,6 @@ describe('Other conditions', function() {
TestService: { TestService: {
unary: function(call, cb) { unary: function(call, cb) {
var req = call.request; var req = call.request;
debugger;
if (req.error) { if (req.error) {
cb(new Error('Requested error'), null, {metadata: ['yes']}); cb(new Error('Requested error'), null, {metadata: ['yes']});
} else { } else {
@ -297,7 +296,7 @@ describe('Other conditions', function() {
misbehavingClient = new Client('localhost:' + port); misbehavingClient = new Client('localhost:' + port);
}); });
it('should respond correctly to a unary call', function(done) { it('should respond correctly to a unary call', function(done) {
var call = misbehavingClient.unary(badArg, function(err, data) { misbehavingClient.unary(badArg, function(err, data) {
assert(err); assert(err);
assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
done(); done();
@ -310,11 +309,13 @@ describe('Other conditions', function() {
done(); done();
}); });
call.write(badArg); call.write(badArg);
// TODO(mlumish): Remove call.end()
call.end();
}); });
it('should respond correctly to a server stream', function(done) { it('should respond correctly to a server stream', function(done) {
var call = misbehavingClient.serverStream(badArg); var call = misbehavingClient.serverStream(badArg);
call.on('data', function(data) { call.on('data', function(data) {
assert.fail(data, null, 'Unexpected data', '!='); assert.fail(data, null, 'Unexpected data', '===');
}); });
call.on('error', function(err) { call.on('error', function(err) {
assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
@ -324,13 +325,15 @@ describe('Other conditions', function() {
it('should respond correctly to a bidi stream', function(done) { it('should respond correctly to a bidi stream', function(done) {
var call = misbehavingClient.bidiStream(); var call = misbehavingClient.bidiStream();
call.on('data', function(data) { call.on('data', function(data) {
assert.fail(data, null, 'Unexpected data', '!='); assert.fail(data, null, 'Unexpected data', '===');
}); });
call.on('error', function(err) { call.on('error', function(err) {
assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
done(); done();
}); });
call.write(badArg); call.write(badArg);
// TODO(mlumish): Remove call.end()
call.end();
}); });
}); });
describe('Trailing metadata', function() { describe('Trailing metadata', function() {

Loading…
Cancel
Save