Repository: kubernetes-client/javascript (ts)

Issue:
List/Watch fails without error if the list function failse
**Describe the bug**
When listFn returns an error, the ListWatch is broken and no ERROR event is sent

**Client Version**
All

**Server Version**
n/a

**To Reproduce**
Fail the initial list function in a list/watch call.

**Expected behavior**
An error should be emitted to the done handler

See #2217 

Pull request description:
Fix a bug where an error on request wouldn't propogate.
Fixes #2222 

Patch:
diff --git a/src/cache.ts b/src/cache.ts
index 8b580d02..2ee8c4f8 100644
--- a/src/cache.ts
+++ b/src/cache.ts
@@ -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';
 
@@ -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 || '';


Test patch:
diff --git a/src/cache_test.ts b/src/cache_test.ts
index bfcd7114..e2e01238 100644
--- a/src/cache_test.ts
+++ b/src/cache_test.ts
@@ -1460,4 +1460,25 @@ 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();
+            });
+        });
+        await lw.start();
+        await errCalled;
+        strictEqual(gotErr, requestErr);
+    });
 });