From 4518058412de5a1d1b7d58087cdcddbd0b0139ee Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Mon, 8 Jul 2024 16:04:24 -0700 Subject: [PATCH] fix(shuttle): Handle missing group key (#2133) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Motivation We didn't handle the case where the group key wasn't already created. ## Change Summary Handle it. ## Merge Checklist - [x] PR title adheres to the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) standard - [x] PR has a [changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets) - [x] PR has been tagged with a change label(s) (i.e. documentation, feature, bugfix, or chore) - [ ] PR includes [documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs) if necessary. - [x] All [commits have been signed](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#22-signing-commits) --- ## PR-Codex overview This PR focuses on gracefully handling "no such key" errors when querying a group on the first start in the `shuttle` package. ### Detailed summary - Added error handling for "no such key" scenario - Skips group creation if key doesn't exist - Improved robustness in handling ReplyError > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --------- Co-authored-by: Ken Goldfarb --- .changeset/calm-rings-lie.md | 5 +++++ packages/shuttle/src/shuttle/eventStream.ts | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/calm-rings-lie.md diff --git a/.changeset/calm-rings-lie.md b/.changeset/calm-rings-lie.md new file mode 100644 index 0000000000..8109da4593 --- /dev/null +++ b/.changeset/calm-rings-lie.md @@ -0,0 +1,5 @@ +--- +"@farcaster/shuttle": patch +--- + +Gracefully handle "no such key" when querying group on first start diff --git a/packages/shuttle/src/shuttle/eventStream.ts b/packages/shuttle/src/shuttle/eventStream.ts index 7cbc98eb53..1a86bfd766 100644 --- a/packages/shuttle/src/shuttle/eventStream.ts +++ b/packages/shuttle/src/shuttle/eventStream.ts @@ -42,11 +42,21 @@ export class EventStreamConnection { * Creates a consumer group for the given stream. */ async createGroup(key: string, consumerGroup: string) { + // Check if the group already exists try { - // Check if the group already exists const groups = (await this.client.xinfo("GROUPS", key)) as [string, string][]; if (groups.some(([_fieldName, groupName]) => groupName === consumerGroup)) return; + } catch (e: unknown) { + if (typeof e === "object" && e !== null && e instanceof ReplyError) { + if ("message" in e && (e.message as string).startsWith("ERR no such key")) { + // Ignore if the group hasn't been created yet + } else { + throw e; + } + } + } + try { // Otherwise create the group return await this.client.xgroup("CREATE", key, consumerGroup, "0", "MKSTREAM"); } catch (e: unknown) {