Skip to content

Commit

Permalink
feat: add error check for null response from API (#1681)
Browse files Browse the repository at this point in the history
* feat: add error check for null response from API

* update kokoro configs to node 18

* actually add files
  • Loading branch information
leahecole authored Dec 16, 2024
1 parent db5cfb5 commit bdfef64
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion gax/src/fallbackRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,19 @@ 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);
throw error;
}
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);
}
21 changes: 21 additions & 0 deletions gax/test/unit/grpc-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand Down

0 comments on commit bdfef64

Please sign in to comment.