Skip to content

Commit fcdd8c9

Browse files
t3chguyGermain
andauthored
[Backport staging] Catch server versions API call exception when starting the client (matrix-org#2832)
Co-authored-by: Germain <[email protected]>
1 parent 7d78033 commit fcdd8c9

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

spec/integ/matrix-client-syncing.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ describe("MatrixClient syncing", () => {
274274

275275
expect(fires).toBe(1);
276276
});
277+
278+
it("should work when all network calls fail", async () => {
279+
httpBackend!.expectedRequests = [];
280+
httpBackend!.when("GET", "").fail(0, new Error("CORS or something"));
281+
const prom = client!.startClient();
282+
await Promise.all([
283+
expect(prom).resolves.toBeUndefined(),
284+
httpBackend!.flushAllExpected(),
285+
]);
286+
});
277287
});
278288

279289
describe("initial sync", () => {

src/client.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,15 +1192,17 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
11921192
this.syncApi.stop();
11931193
}
11941194

1195-
const serverVersions = await this.getVersions();
1196-
this.canSupport = await buildFeatureSupportMap(serverVersions);
1197-
1198-
const support = this.canSupport.get(Feature.ThreadUnreadNotifications);
1199-
UNREAD_THREAD_NOTIFICATIONS.setPreferUnstable(support === ServerSupport.Unstable);
1195+
try {
1196+
await this.getVersions();
12001197

1201-
const { threads, list } = await this.doesServerSupportThread();
1202-
Thread.setServerSideSupport(threads);
1203-
Thread.setServerSideListSupport(list);
1198+
// This should be done with `canSupport`
1199+
// TODO: https://github.com/vector-im/element-web/issues/23643
1200+
const { threads, list } = await this.doesServerSupportThread();
1201+
Thread.setServerSideSupport(threads);
1202+
Thread.setServerSideListSupport(list);
1203+
} catch (e) {
1204+
logger.error("Can't fetch server versions, continuing to initialise sync, this will be retried later", e);
1205+
}
12041206

12051207
// shallow-copy the opts dict before modifying and storing it
12061208
this.clientOpts = Object.assign({}, opts) as IStoredClientOpts;
@@ -6518,7 +6520,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
65186520
* unstable APIs it supports
65196521
* @return {Promise<object>} The server /versions response
65206522
*/
6521-
public getVersions(): Promise<IServerVersions> {
6523+
public async getVersions(): Promise<IServerVersions> {
65226524
if (this.serverVersionsPromise) {
65236525
return this.serverVersionsPromise;
65246526
}
@@ -6530,13 +6532,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
65306532
{
65316533
prefix: '',
65326534
},
6533-
).catch((e: Error) => {
6535+
).catch(e => {
65346536
// Need to unset this if it fails, otherwise we'll never retry
65356537
this.serverVersionsPromise = null;
65366538
// but rethrow the exception to anything that was waiting
65376539
throw e;
65386540
});
65396541

6542+
const serverVersions = await this.serverVersionsPromise;
6543+
this.canSupport = await buildFeatureSupportMap(serverVersions);
6544+
6545+
// We can set flag values to use their stable or unstable version
6546+
const support = this.canSupport.get(Feature.ThreadUnreadNotifications);
6547+
UNREAD_THREAD_NOTIFICATIONS.setPreferUnstable(support === ServerSupport.Unstable);
6548+
65406549
return this.serverVersionsPromise;
65416550
}
65426551

0 commit comments

Comments
 (0)