Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return error if GetOperation call fails #1304

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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