From c4c47c0e6cbe842d17604e47457d6b9a4d135074 Mon Sep 17 00:00:00 2001 From: Miles Date: Thu, 14 Jul 2022 21:15:14 +0100 Subject: [PATCH] fix: handle fetch errors --- src/fallbackServiceStub.ts | 3 ++- test/unit/grpc-fallback.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/fallbackServiceStub.ts b/src/fallbackServiceStub.ts index 78936d218..e085cf6c2 100644 --- a/src/fallbackServiceStub.ts +++ b/src/fallbackServiceStub.ts @@ -171,7 +171,8 @@ export function generateServiceStub( } }); } - }); + }) + .catch((err: unknown) => callback(err)); if (rpc.responseStream) { return streamArrayParser; diff --git a/test/unit/grpc-fallback.ts b/test/unit/grpc-fallback.ts index d806271e6..15aae44a7 100644 --- a/test/unit/grpc-fallback.ts +++ b/test/unit/grpc-fallback.ts @@ -284,7 +284,7 @@ describe('grpc-fallback', () => { }); }); - it('should handle an error', done => { + it('should handle an API error', done => { const requestObject = {content: 'test-content'}; // example of an actual google.rpc.Status error message returned by Language API const fixtureName = path.resolve(__dirname, '..', 'fixtures', 'error.bin'); @@ -327,6 +327,18 @@ describe('grpc-fallback', () => { }); }); + it('should handle a fetch error', done => { + const requestObject = {content: 'test-content'}; + //@ts-ignore + sinon.stub(nodeFetch, 'Promise').rejects(new Error('fetch error')); + gaxGrpc.createStub(echoService, stubOptions).then(echoStub => { + echoStub.echo(requestObject, {}, {}, (err?: Error) => { + assert.strictEqual(err?.message, 'fetch error'); + done(); + }); + }); + }); + it('should promote ErrorInfo if exist in fallback-rest error', done => { const requestObject = {content: 'test-content'}; // example of an actual google.rpc.Status error message returned by Translate API