From 0158f0be56c7e63275ca6a35bae295d91dc9460a Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Fri, 26 Oct 2018 15:37:01 -0700 Subject: [PATCH] Make clients fail better with malformed responses --- .../src/client_interceptors.js | 27 +++++++++++++++---- .../grpc-native-core/test/surface_test.js | 4 --- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/grpc-native-core/src/client_interceptors.js b/packages/grpc-native-core/src/client_interceptors.js index f240f9d3..fc371e5c 100644 --- a/packages/grpc-native-core/src/client_interceptors.js +++ b/packages/grpc-native-core/src/client_interceptors.js @@ -382,6 +382,8 @@ function InterceptingCall(next_call, requester) { this.requester = requester; } +const emptyNext = function() {}; + /** * Get the next method in the chain or a no-op function if we are at the end * of the chain @@ -392,7 +394,7 @@ function InterceptingCall(next_call, requester) { InterceptingCall.prototype._getNextCall = function(method_name) { return this.next_call ? this.next_call[method_name].bind(this.next_call) : - function(){}; + emptyNext; }; /** @@ -421,6 +423,9 @@ InterceptingCall.prototype._callNext = function(method_name, args, next) { next_call); } } else { + if (next_call === emptyNext) { + throw new Error('Interceptor call chain terminated unexpectedly'); + } return next_call(args_array[0], args_array[1]); } }; @@ -476,11 +481,11 @@ InterceptingCall.prototype.cancel = function() { /** * Run a cancelWithStatus operation through the interceptor chain. - * @param {grpc~StatusObject} status - * @param {string} message + * @param {number} code + * @param {string} details */ -InterceptingCall.prototype.cancelWithStatus = function(status, message) { - this._callNext('cancelWithStatus', [status, message]); +InterceptingCall.prototype.cancelWithStatus = function(code, details) { + this._callNext('cancelWithStatus', [code, details]); }; /** @@ -845,6 +850,9 @@ function _getUnaryInterceptor(method_definition, channel, emitter, callback) { final_requester.cancel = function () { call.cancel(); }; + final_requester.cancelWithStatus = function(code, details) { + call.cancelWithStatus(code, details) + }; final_requester.getPeer = function () { return call.getPeer(); }; @@ -957,6 +965,9 @@ function _getClientStreamingInterceptor(method_definition, channel, emitter, final_requester.cancel = function () { call.cancel(); }; + final_requester.cancelWithStatus = function(code, details) { + call.cancelWithStatus(code, details) + }; final_requester.getPeer = function() { return call.getPeer(); }; @@ -1053,6 +1064,9 @@ function _getServerStreamingInterceptor(method_definition, channel, emitter) { final_requester.cancel = function() { call.cancel(); }; + final_requester.cancelWithStatus = function(code, details) { + call.cancelWithStatus(code, details) + }; final_requester.getPeer = function() { return call.getPeer(); }; @@ -1159,6 +1173,9 @@ function _getBidiStreamingInterceptor(method_definition, channel, emitter) { final_requester.cancel = function() { call.cancel(); }; + final_requester.cancelWithStatus = function(code, details) { + call.cancelWithStatus(code, details) + }; final_requester.getPeer = function() { return call.getPeer(); }; diff --git a/packages/grpc-native-core/test/surface_test.js b/packages/grpc-native-core/test/surface_test.js index aa124620..ba0f1d52 100644 --- a/packages/grpc-native-core/test/surface_test.js +++ b/packages/grpc-native-core/test/surface_test.js @@ -666,16 +666,12 @@ describe('Client malformed response handling', function() { }, serverStream: function(stream) { stream.write(badArg); - stream.end(); }, bidiStream: function(stream) { stream.on('data', function() { // Ignore requests stream.write(badArg); }); - stream.on('end', function() { - stream.end(); - }); } }); var port = server.bind('localhost:0', server_insecure_creds);