Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ObjectCallback,
UPDATE,
} from './informer.js';
import { KubernetesObject } from './types.js';
import { KubernetesObject, KubernetesListObject } from './types.js';
import { ObjectSerializer } from './serializer.js';
import { Watch } from './watch.js';

Expand Down Expand Up @@ -143,8 +143,14 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
}
this.callbackCache[CONNECT].forEach((elt: ErrorCallback) => elt(undefined));
if (!this.resourceVersion) {
const promise = this.listFn();
const list = await promise;
let list: KubernetesListObject<T>;
try {
const promise = this.listFn();
list = await promise;
} catch (err) {
this.callbackCache[ERROR].forEach((elt: ErrorCallback) => elt(err));
return;
}
this.objects = deleteItems(this.objects, list.items, this.callbackCache[DELETE].slice());
this.addOrUpdateItems(list.items);
this.resourceVersion = list.metadata!.resourceVersion || '';
Expand Down
27 changes: 26 additions & 1 deletion src/cache_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepStrictEqual, notStrictEqual, strictEqual, throws } from 'node:assert';
import { deepStrictEqual, fail, equal, notStrictEqual, strictEqual, throws } from 'node:assert';
import mock from 'ts-mockito';

import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1PodList, V1ListMeta } from './api.js';
Expand Down Expand Up @@ -1460,4 +1460,29 @@ describe('delete items', () => {

strictEqual(await connectPromise, true);
});

it('should correctly handle errors in the initial list', async () => {
const fake = mock.mock(Watch);
const requestErr = Error('request failed');
const listFn: ListPromise<V1Namespace> = function (): Promise<V1NamespaceList> {
return new Promise<V1NamespaceList>((resolve, reject) => {
reject(requestErr);
});
};
const lw = new ListWatch('/some/path', fake, listFn);
let gotErr: Error | null = null;
const errCalled = new Promise<void>((resolve, reject) => {
lw.on('error', (err) => {
gotErr = err;
resolve();
});
});
try {
await lw.start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the try...catch since the unexpected error will fail the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

await errCalled;
equal(gotErr, requestErr);
} catch (err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we want a === check here:

Suggested change
equal(gotErr, requestErr);
strictEqual(gotErr, requestErr);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (and removed the import at the top)

fail(`unexpected error: ${err}`);
}
});
});
Loading