Skip to content

Commit 491366b

Browse files
fix: filter subscription with pubsubTopic1 and decoder with pubsubTopic2 (#1675)
* add a test for a failing case * add error handling & update test * remove only * rename test
1 parent 81b2bf4 commit 491366b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/core/src/lib/filter/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ class Subscription {
7474
callback: Callback<T>
7575
): Promise<void> {
7676
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
77+
78+
// check that all decoders are configured for the same pubsub topic as this subscription
79+
decodersArray.forEach((decoder) => {
80+
if (decoder.pubsubTopic !== this.pubsubTopic) {
81+
throw new Error(
82+
`Pubsub topic not configured: decoder is configured for pubsub topic ${decoder.pubsubTopic} but this subscription is for pubsub topic ${this.pubsubTopic}. Please create a new Subscription for the different pubsub topic.`
83+
);
84+
}
85+
});
86+
7787
const decodersGroupedByCT = groupByContentTopic(decodersArray);
7888
const contentTopics = Array.from(decodersGroupedByCT.keys());
7989

packages/tests/tests/filter/multiple_pubsub.node.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,15 @@ describe("Waku Filter V2: Multiple PubSubtopics", function () {
146146
expectedMessageText: "M2"
147147
});
148148
});
149+
150+
it("Should fail to subscribe with decoder with wrong pubsubTopic", async function () {
151+
// this subscription object is set up with the `customPubsubTopic` but we're passing it a Decoder with the `DefaultPubsubTopic`
152+
try {
153+
await subscription.subscribe([TestDecoder], messageCollector.callback);
154+
} catch (error) {
155+
expect((error as Error).message).to.include(
156+
"Pubsub topic not configured"
157+
);
158+
}
159+
});
149160
});

0 commit comments

Comments
 (0)