diff --git a/gax/src/fallbackRest.ts b/gax/src/fallbackRest.ts index 587504a07..2abb5ec59 100644 --- a/gax/src/fallbackRest.ts +++ b/gax/src/fallbackRest.ts @@ -85,6 +85,9 @@ export function decodeResponse( ): {} { // eslint-disable-next-line n/no-unsupported-features/node-builtins const decodedString = new TextDecoder().decode(response); + if (!decodedString) { + throw new Error(`Received null response from RPC ${rpc.name}`); + } const json = JSON.parse(decodedString); if (!ok) { const error = GoogleError.parseHttpError(json); @@ -92,7 +95,9 @@ export function decodeResponse( } const message = serializer.fromProto3JSON(rpc.resolvedResponseType!, json); if (!message) { - throw new Error(`Received null response from RPC ${rpc.name}`); + throw new Error( + `Received null or malformed response from JSON serializer from RPC ${rpc.name}` + ); } return rpc.resolvedResponseType!.toObject(message, defaultToObjectOptions); } diff --git a/gax/test/unit/grpc-fallback.ts b/gax/test/unit/grpc-fallback.ts index 17f67590d..0c3c94952 100644 --- a/gax/test/unit/grpc-fallback.ts +++ b/gax/test/unit/grpc-fallback.ts @@ -356,6 +356,27 @@ describe('grpc-fallback', () => { }); }); }); + it('should handle a null response from the API ', done => { + const requestObject = {content: 'test-content'}; + const expectedMessage = 'Received null response from RPC Echo'; + + //@ts-ignore + sinon.stub(nodeFetch, 'Promise').returns( + Promise.resolve({ + ok: false, + arrayBuffer: () => { + return Promise.resolve(Buffer.from('')); + }, + }) + ); + gaxGrpc.createStub(echoService, stubOptions).then(echoStub => { + echoStub.echo(requestObject, {}, {}, (err?: Error) => { + assert(err instanceof Error); + assert.strictEqual(err.message, expectedMessage); + done(); + }); + }); + }); it('should handle a fetch error', done => { const requestObject = {content: 'test-content'};