From 89f57ff879083a3c7d95b3a46ba448c775faa280 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 7 Aug 2024 11:50:00 -0500 Subject: [PATCH] Fix retries in threadgate too, add comments --- src/state/queries/postgate/index.ts | 11 ++++++++- src/state/queries/threadgate/index.ts | 32 +++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/state/queries/postgate/index.ts b/src/state/queries/postgate/index.ts index b7b4abe9fc..c4d5c7a0ce 100644 --- a/src/state/queries/postgate/index.ts +++ b/src/state/queries/postgate/index.ts @@ -35,10 +35,14 @@ export async function getPostgateRecord({ } try { - // TODO don't retry on 404 const {data} = await retry( 2, e => { + /* + * If the record doesn't exist, we want to return null instead of + * throwing an error. NB: This will also catch reference errors, such as + * a typo in the URI. + */ if (e.message.includes(`Could not locate record:`)) { return false } @@ -58,6 +62,11 @@ export async function getPostgateRecord({ return undefined } } catch (e: any) { + /* + * If the record doesn't exist, we want to return null instead of + * throwing an error. NB: This will also catch reference errors, such as + * a typo in the URI. + */ if (e.message.includes(`Could not locate record:`)) { return undefined } else { diff --git a/src/state/queries/threadgate/index.ts b/src/state/queries/threadgate/index.ts index e7ec3689e1..48cf4c4323 100644 --- a/src/state/queries/threadgate/index.ts +++ b/src/state/queries/threadgate/index.ts @@ -1,7 +1,7 @@ import {AppBskyFeedThreadgate, AtUri, BskyAgent} from '@atproto/api' import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' -import {networkRetry} from '#/lib/async/retry' +import {networkRetry, retry} from '#/lib/async/retry' import {STALE} from '#/state/queries' import {ThreadgateAllowUISetting} from '#/state/queries/threadgate/types' import { @@ -60,12 +60,25 @@ export async function getThreadgateRecord({ } try { - const {data} = await networkRetry(2, () => - agent.api.com.atproto.repo.getRecord({ - repo: urip.host, - collection: 'app.bsky.feed.threadgate', - rkey: urip.rkey, - }), + const {data} = await retry( + 2, + e => { + /* + * If the record doesn't exist, we want to return null instead of + * throwing an error. NB: This will also catch reference errors, such as + * a typo in the URI. + */ + if (e.message.includes(`Could not locate record:`)) { + return false + } + return true + }, + () => + agent.api.com.atproto.repo.getRecord({ + repo: urip.host, + collection: 'app.bsky.feed.threadgate', + rkey: urip.rkey, + }), ) if (data.value && AppBskyFeedThreadgate.isRecord(data.value)) { @@ -74,6 +87,11 @@ export async function getThreadgateRecord({ return null } } catch (e: any) { + /* + * If the record doesn't exist, we want to return null instead of + * throwing an error. NB: This will also catch reference errors, such as + * a typo in the URI. + */ if (e.message.includes(`Could not locate record:`)) { return null } else {