Skip to content

Commit

Permalink
feat: add error check for null response from API
Browse files Browse the repository at this point in the history
  • Loading branch information
leahecole committed Dec 10, 2024
1 parent db5cfb5 commit fa6b8f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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 fa6b8f2

Please sign in to comment.