Skip to content

Commit

Permalink
fix: return error if GetOperation call fails (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster authored Aug 17, 2022
1 parent 6b12234 commit cb21ced
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/longRunningCalls/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,20 @@ export class Operation extends EventEmitter {
this._callOptions!
);

const noCallbackPromise = this.currentCallPromise_!.then(responses => {
self.latestResponse = responses[0] as LROOperation;
self._unpackResponse(responses[0] as LROOperation, callback);
return promisifyResponse()!;
});
const noCallbackPromise = this.currentCallPromise_.then(
responses => {
self.latestResponse = responses[0] as LROOperation;
self._unpackResponse(responses[0] as LROOperation, callback);
return promisifyResponse()!;
},
(err: Error) => {
if (callback) {
callback(err);
return;
}
return Promise.reject(err);
}
);

if (!callback) {
return noCallbackPromise as Promise<{}>;
Expand Down
45 changes: 42 additions & 3 deletions test/unit/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,20 @@ describe('longrunning', () => {
let remainingCalls = opts.expectedCalls ? opts.expectedCalls : null;
const cancelGetOperationSpy = sinon.spy();
const getOperationSpy = sinon.spy(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let resolver: any;
const promise = new Promise(resolve => {
let resolver: (value: unknown) => void = () => {};
let rejecter: (value: unknown) => void = () => {};
const promise = new Promise((resolve, reject) => {
resolver = resolve;
rejecter = reject;
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(promise as any).cancel = cancelGetOperationSpy;

if (remainingCalls && remainingCalls > 1) {
resolver([PENDING_OP]);
--remainingCalls;
} else if (opts.reject) {
rejecter(opts.reject);
} else if (!opts.dontResolve) {
resolver([opts.finalOperation || SUCCESSFUL_OP]);
}
Expand Down Expand Up @@ -602,6 +605,42 @@ describe('longrunning', () => {
});
});

it('getOperation failure emits an error', done => {
const func = (
argument: {},
metadata: {},
options: {},
callback: Function
) => {
callback(null, PENDING_OP);
};
const expectedCalls = 3;
const googleError = new GoogleError('GetOperation call failed');
googleError.code = 8;
googleError.statusDetails = 'Quota exceeded';
const client = mockOperationsClient({
expectedCalls,
reject: googleError,
});
const apiCall = createApiCall(func, client);
apiCall({})
.then(responses => {
const operation = responses[0] as longrunning.Operation;
operation.on('complete', () => {
done(new Error('Should not get here.'));
});
operation.on('error', err => {
assert.strictEqual(client.getOperation.callCount, expectedCalls);
assert.strictEqual(err.code, googleError.code);
assert.strictEqual(err.message, googleError.message);
done();
});
})
.catch(err => {
done(err);
});
});

it('emits progress on updated operations.', done => {
const func = (
argument: {},
Expand Down

0 comments on commit cb21ced

Please sign in to comment.