Skip to content

Commit bdfef64

Browse files
authored
feat: add error check for null response from API (#1681)
* feat: add error check for null response from API * update kokoro configs to node 18 * actually add files
1 parent db5cfb5 commit bdfef64

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

.kokoro/continuous/node14/browser-test.cfg .kokoro/continuous/node18/browser-test.cfg

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.kokoro/presubmit/node14/browser-test.cfg .kokoro/presubmit/node18/browser-test.cfg

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gax/src/fallbackRest.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,19 @@ export function decodeResponse(
8585
): {} {
8686
// eslint-disable-next-line n/no-unsupported-features/node-builtins
8787
const decodedString = new TextDecoder().decode(response);
88+
if (!decodedString) {
89+
throw new Error(`Received null response from RPC ${rpc.name}`);
90+
}
8891
const json = JSON.parse(decodedString);
8992
if (!ok) {
9093
const error = GoogleError.parseHttpError(json);
9194
throw error;
9295
}
9396
const message = serializer.fromProto3JSON(rpc.resolvedResponseType!, json);
9497
if (!message) {
95-
throw new Error(`Received null response from RPC ${rpc.name}`);
98+
throw new Error(
99+
`Received null or malformed response from JSON serializer from RPC ${rpc.name}`
100+
);
96101
}
97102
return rpc.resolvedResponseType!.toObject(message, defaultToObjectOptions);
98103
}

gax/test/unit/grpc-fallback.ts

+21
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,27 @@ describe('grpc-fallback', () => {
356356
});
357357
});
358358
});
359+
it('should handle a null response from the API ', done => {
360+
const requestObject = {content: 'test-content'};
361+
const expectedMessage = 'Received null response from RPC Echo';
362+
363+
//@ts-ignore
364+
sinon.stub(nodeFetch, 'Promise').returns(
365+
Promise.resolve({
366+
ok: false,
367+
arrayBuffer: () => {
368+
return Promise.resolve(Buffer.from(''));
369+
},
370+
})
371+
);
372+
gaxGrpc.createStub(echoService, stubOptions).then(echoStub => {
373+
echoStub.echo(requestObject, {}, {}, (err?: Error) => {
374+
assert(err instanceof Error);
375+
assert.strictEqual(err.message, expectedMessage);
376+
done();
377+
});
378+
});
379+
});
359380

360381
it('should handle a fetch error', done => {
361382
const requestObject = {content: 'test-content'};

0 commit comments

Comments
 (0)