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

Catch server versions API call exception when starting the client #2828

Merged
merged 6 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions spec/integ/matrix-client-syncing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
const prom = client!.startClient();
await Promise.all([
expect(prom).resolves.toBeUndefined(),
httpBackend!.flushAllExpected(),
]);
});
});

describe("initial sync", () => {
Expand Down
33 changes: 21 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,16 +1193,18 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
this.syncApi.stop();
}

const serverVersions = await this.getVersions();
this.canSupport = await buildFeatureSupportMap(serverVersions);

const support = this.canSupport.get(Feature.ThreadUnreadNotifications);
UNREAD_THREAD_NOTIFICATIONS.setPreferUnstable(support === ServerSupport.Unstable);

const { threads, list, fwdPagination } = await this.doesServerSupportThread();
Thread.setServerSideSupport(threads);
Thread.setServerSideListSupport(list);
Thread.setServerSideFwdPaginationSupport(fwdPagination);
try {
await this.getVersions();

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

// shallow-copy the opts dict before modifying and storing it
this.clientOpts = Object.assign({}, opts) as IStoredClientOpts;
Expand Down Expand Up @@ -6712,7 +6714,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* unstable APIs it supports
* @return {Promise<object>} The server /versions response
*/
public getVersions(): Promise<IServerVersions> {
public async getVersions(): Promise<IServerVersions> {
if (this.serverVersionsPromise) {
return this.serverVersionsPromise;
}
Expand All @@ -6724,13 +6726,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
{
prefix: '',
},
).catch((e: Error) => {
).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;
}

Expand Down