mirror of https://github.com/grpc/grpc-node.git
Fixed server to handle invalid arguments without breaking
This commit is contained in:
parent
06ddb92e9c
commit
18b727d54a
|
@ -291,7 +291,15 @@ function _read(size) {
|
|||
return;
|
||||
}
|
||||
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 = {};
|
||||
read_batch[grpc.opType.RECV_MESSAGE] = true;
|
||||
self.call.startBatch(read_batch, readCallback);
|
||||
|
@ -354,7 +362,13 @@ function handleUnary(call, handler, metadata) {
|
|||
handleError(call, err);
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
@ -388,7 +402,13 @@ function handleServerStreaming(call, handler, metadata) {
|
|||
stream.emit('error', err);
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
@ -401,6 +421,9 @@ function handleServerStreaming(call, handler, metadata) {
|
|||
*/
|
||||
function handleClientStreaming(call, handler, metadata) {
|
||||
var stream = new ServerReadableStream(call, handler.deserialize);
|
||||
stream.on('error', function(error) {
|
||||
handleError(call, error);
|
||||
});
|
||||
waitForCancel(call, stream);
|
||||
var metadata_batch = {};
|
||||
metadata_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata;
|
||||
|
|
|
@ -192,7 +192,6 @@ describe('Other conditions', function() {
|
|||
TestService: {
|
||||
unary: function(call, cb) {
|
||||
var req = call.request;
|
||||
debugger;
|
||||
if (req.error) {
|
||||
cb(new Error('Requested error'), null, {metadata: ['yes']});
|
||||
} else {
|
||||
|
@ -297,7 +296,7 @@ describe('Other conditions', function() {
|
|||
misbehavingClient = new Client('localhost:' + port);
|
||||
});
|
||||
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.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
|
||||
done();
|
||||
|
@ -310,11 +309,13 @@ describe('Other conditions', function() {
|
|||
done();
|
||||
});
|
||||
call.write(badArg);
|
||||
// TODO(mlumish): Remove call.end()
|
||||
call.end();
|
||||
});
|
||||
it('should respond correctly to a server stream', function(done) {
|
||||
var call = misbehavingClient.serverStream(badArg);
|
||||
call.on('data', function(data) {
|
||||
assert.fail(data, null, 'Unexpected data', '!=');
|
||||
assert.fail(data, null, 'Unexpected data', '===');
|
||||
});
|
||||
call.on('error', function(err) {
|
||||
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) {
|
||||
var call = misbehavingClient.bidiStream();
|
||||
call.on('data', function(data) {
|
||||
assert.fail(data, null, 'Unexpected data', '!=');
|
||||
assert.fail(data, null, 'Unexpected data', '===');
|
||||
});
|
||||
call.on('error', function(err) {
|
||||
assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
|
||||
done();
|
||||
});
|
||||
call.write(badArg);
|
||||
// TODO(mlumish): Remove call.end()
|
||||
call.end();
|
||||
});
|
||||
});
|
||||
describe('Trailing metadata', function() {
|
||||
|
|
Loading…
Reference in New Issue