Skip to content

Commit

Permalink
Merge pull request #5433 from WalletConnect/fix/subscriber-publish-fail
Browse files Browse the repository at this point in the history
fix: subscriber publish fail
  • Loading branch information
ganchoradkov authored Oct 9, 2024
2 parents cb6a978 + 70cee4c commit 304b77a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/core/src/controllers/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class Subscriber extends ISubscriber {
};
this.logger.debug(`Outgoing Relay Payload`);
this.logger.trace({ type: "payload", direction: "outgoing", request });
const shouldThrow = opts?.internal?.throwOnFailedPublish;
try {
const subId = hashMessage(topic + this.clientId);
// in link mode, allow the app to update its network state (i.e. active airplane mode) with small delay before attempting to subscribe
Expand All @@ -253,12 +254,15 @@ export class Subscriber extends ISubscriber {
`Subscribing to ${topic} failed, please try again`,
);
const result = await subscribe;
if (!result && shouldThrow) {
throw new Error(`Subscribing to ${topic} failed, please try again`);
}
// return null to indicate that the subscription failed
return result ? subId : null;
} catch (err) {
this.logger.debug(`Outgoing Relay Subscribe Payload stalled`);
this.relayer.events.emit(RELAYER_EVENTS.connection_stalled);
if (opts?.internal?.throwOnFailedPublish) {
if (shouldThrow) {
throw err;
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/core/test/relayer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ describe("Relayer", () => {
);
});

it("should throw when subscribe publish fails", async () => {
await relayer.transportOpen();
await relayer.toEstablishConnection();
relayer.subscriber.subscribeTimeout = 5_000;
relayer.request = () => {
return new Promise<void>((resolve) => {
resolve();
});
};
const topic = generateRandomBytes32();
await expect(relayer.subscribe(topic)).rejects.toThrow(
`Subscribing to ${topic} failed, please try again`,
);
});

it("should be able to resubscribe on topic that already exists", async () => {
const topic = generateRandomBytes32();
const id = await relayer.subscribe(topic);
Expand Down

0 comments on commit 304b77a

Please sign in to comment.