From 6bf69eb1435f495dad7625ae1db79b06c0bea4d5 Mon Sep 17 00:00:00 2001 From: Germain Date: Mon, 31 Oct 2022 16:56:14 +0000 Subject: [PATCH 1/6] Catch server versions API call exception when starting the client --- src/client.ts | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/client.ts b/src/client.ts index 67d56f7b942..d0ca25141e2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1193,11 +1193,11 @@ export class MatrixClient extends TypedEventEmitter} The server /versions response */ - public getVersions(): Promise { + public async getVersions(): Promise { if (this.serverVersionsPromise) { return this.serverVersionsPromise; } - this.serverVersionsPromise = this.http.request( - Method.Get, "/_matrix/client/versions", - undefined, // queryParams - undefined, // data - { - prefix: '', - }, - ).catch((e: Error) => { + try { + this.serverVersionsPromise = this.http.request( + Method.Get, "/_matrix/client/versions", + undefined, // queryParams + undefined, // data + { + prefix: '', + }, + ); + // throw "YOLO"; + } catch (e) { // Need to unset this if it fails, otherwise we'll never retry this.serverVersionsPromise = undefined; // but rethrow the exception to anything that was waiting throw e; - }); + } + + const serverVersions = await this.serverVersionsPromise; + this.canSupport = await buildFeatureSupportMap(serverVersions); + + // We can set flag values to use their stable or unstable version + const support = this.canSupport.get(Feature.ThreadUnreadNotifications); + UNREAD_THREAD_NOTIFICATIONS.setPreferUnstable(support === ServerSupport.Unstable); return this.serverVersionsPromise; } From c839779f78adcadfd984cb26854924b4bcaee0a2 Mon Sep 17 00:00:00 2001 From: Germain Date: Mon, 31 Oct 2022 17:03:17 +0000 Subject: [PATCH 2/6] Remove log line --- src/client.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index d0ca25141e2..7ff2fe17cc4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6726,7 +6726,6 @@ export class MatrixClient extends TypedEventEmitter Date: Mon, 31 Oct 2022 17:07:02 +0000 Subject: [PATCH 3/6] Add test when all API call fail in startClient --- spec/integ/matrix-client-syncing.spec.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/integ/matrix-client-syncing.spec.ts b/spec/integ/matrix-client-syncing.spec.ts index 3609e8a0f3d..d9099444273 100644 --- a/spec/integ/matrix-client-syncing.spec.ts +++ b/spec/integ/matrix-client-syncing.spec.ts @@ -274,6 +274,15 @@ describe("MatrixClient syncing", () => { expect(fires).toBe(1); }); + + it("should work when all network calls fail", async () => { + httpBackend!.when("GET", "").fail(0, new Error("CORS or something")); + const prom = client!.startClient(); + await Promise.all([ + expect(prom).resolves.toBeUndefined(), + httpBackend!.flushAllExpected(), + ]); + }); }); describe("initial sync", () => { From 44e8fbcb12fdac5d7e2dd1674e061bd4999e867c Mon Sep 17 00:00:00 2001 From: Germain Date: Mon, 31 Oct 2022 17:18:36 +0000 Subject: [PATCH 4/6] Fix try/catch in getVersions --- src/client.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/client.ts b/src/client.ts index 7ff2fe17cc4..3b752e77ab6 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6717,21 +6717,19 @@ export class MatrixClient extends TypedEventEmitter( - Method.Get, "/_matrix/client/versions", - undefined, // queryParams - undefined, // data - { - prefix: '', - }, - ); - } catch (e) { + this.serverVersionsPromise = this.http.request( + Method.Get, "/_matrix/client/versions", + undefined, // queryParams + undefined, // data + { + prefix: '', + }, + ).catch(e => { // Need to unset this if it fails, otherwise we'll never retry this.serverVersionsPromise = undefined; // but rethrow the exception to anything that was waiting throw e; - } + }); const serverVersions = await this.serverVersionsPromise; this.canSupport = await buildFeatureSupportMap(serverVersions); From c273b9813dd2f6c5174245f3f31d6c97d8c85eb6 Mon Sep 17 00:00:00 2001 From: Germain Date: Mon, 31 Oct 2022 17:29:39 +0000 Subject: [PATCH 5/6] Move threads feature detection in try/catch --- src/client.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/client.ts b/src/client.ts index 3b752e77ab6..4c380ec4138 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1195,15 +1195,17 @@ export class MatrixClient extends TypedEventEmitter Date: Mon, 31 Oct 2022 17:34:41 +0000 Subject: [PATCH 6/6] reset expected requests to sure /versions fails --- spec/integ/matrix-client-syncing.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/integ/matrix-client-syncing.spec.ts b/spec/integ/matrix-client-syncing.spec.ts index d9099444273..418e2d16add 100644 --- a/spec/integ/matrix-client-syncing.spec.ts +++ b/spec/integ/matrix-client-syncing.spec.ts @@ -276,6 +276,7 @@ describe("MatrixClient syncing", () => { }); it("should work when all network calls fail", async () => { + httpBackend!.expectedRequests = []; httpBackend!.when("GET", "").fail(0, new Error("CORS or something")); const prom = client!.startClient(); await Promise.all([