Skip to content

Commit 34529ec

Browse files
committed
refactor(search): flatten live read failure classification
1 parent e62c179 commit 34529ec

1 file changed

Lines changed: 47 additions & 49 deletions

File tree

‎apps/sim/lib/sim-search/live/application.ts‎

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
} from '@/lib/sim-search/live/account-session'
4343
import {
4444
listLiveAccounts,
45-
type ResolvedLiveAccount,
4645
resolveListedLiveAccount,
4746
resolveLiveAccount,
4847
} from '@/lib/sim-search/live/accounts'
@@ -814,59 +813,58 @@ export const readLiveDocument = defineAuthorizedKnowledgeUseCase({
814813
(input.filters?.documentIds && !input.filters.documentIds.includes(input.documentId))
815814
)
816815
throw new OrchestrationError('not_found', 'Document is outside the selected search filters')
817-
let resolved: ResolvedLiveAccount
816+
/** Cancellation by the caller passes through unclassified. */
817+
const readFailure = (error: unknown, deadline?: AbortSignal) =>
818+
input.signal?.aborted ? error : liveReadFailure(error, reference.provider, deadline)
819+
const [resolved, policies] = await Promise.all([
820+
resolveLiveAccount(input, userId, reference.account),
821+
loadLiveSearchPolicies(input),
822+
]).catch((error: unknown) => {
823+
throw readFailure(error)
824+
})
825+
if (resolved.account.provider !== reference.provider)
826+
throw new OrchestrationError('not_found', 'Document account changed')
827+
const deadline = AbortSignal.timeout(READ_DEADLINE_MS)
828+
const signal = input.signal ? AbortSignal.any([input.signal, deadline]) : deadline
829+
const pool = createPinnedConnectionPool()
818830
let document: NativeDocument
819-
let deadline: AbortSignal | undefined
831+
let session: LiveAccountSession | undefined
820832
try {
821-
const [account, policies] = await Promise.all([
822-
resolveLiveAccount(input, userId, reference.account),
823-
loadLiveSearchPolicies(input),
824-
])
825-
resolved = account
826-
if (resolved.account.provider !== reference.provider)
827-
throw new OrchestrationError('not_found', 'Document account changed')
828-
deadline = AbortSignal.timeout(READ_DEADLINE_MS)
829-
const signal = input.signal ? AbortSignal.any([input.signal, deadline]) : deadline
830-
const pool = createPinnedConnectionPool()
831-
let session: LiveAccountSession | undefined
832-
try {
833-
session = await openLiveAccountSession({
834-
owner: input,
835-
userId,
836-
resolved,
837-
policies,
838-
signal,
839-
pool,
840-
})
841-
if (!(await session.verify(reference)))
842-
throw new OrchestrationError(
843-
'not_found',
844-
'Document is outside your organization’s search scope'
845-
)
846-
const currentSession = session
847-
document = await measureSearchStage('live.read', () =>
848-
currentSession.read(reference, input.filters)
833+
session = await openLiveAccountSession({
834+
owner: input,
835+
userId,
836+
resolved,
837+
policies,
838+
signal,
839+
pool,
840+
})
841+
if (!(await session.verify(reference)))
842+
throw new OrchestrationError(
843+
'not_found',
844+
'Document is outside your organization’s search scope'
849845
)
850-
/** Readers degrade section failures to warnings, so the signal decides cancellation. */
851-
signal.throwIfAborted()
852-
const current = await session.verifyCurrent(document)
853-
/** A verifier may report a check cut short by cancellation as a normal result. */
854-
signal.throwIfAborted()
855-
if (!current)
856-
throw new OrchestrationError(
857-
'not_found',
858-
'Document is outside your organization’s search scope'
859-
)
846+
const currentSession = session
847+
document = await measureSearchStage('live.read', () =>
848+
currentSession.read(reference, input.filters)
849+
)
850+
/** Readers degrade section failures to warnings, so the signal decides cancellation. */
851+
signal.throwIfAborted()
852+
const current = await session.verifyCurrent(document)
853+
/** A verifier may report a check cut short by cancellation as a normal result. */
854+
signal.throwIfAborted()
855+
if (!current)
856+
throw new OrchestrationError(
857+
'not_found',
858+
'Document is outside your organization’s search scope'
859+
)
860+
} catch (error) {
861+
throw readFailure(error, deadline)
862+
} finally {
863+
try {
864+
await session?.close()
860865
} finally {
861-
try {
862-
await session?.close()
863-
} finally {
864-
pool.destroy()
865-
}
866+
pool.destroy()
866867
}
867-
} catch (error) {
868-
if (input.signal?.aborted) throw error
869-
throw liveReadFailure(error, reference.provider, deadline)
870868
}
871869
if (!matchesLiveFilters(document, input.documentId, reference.provider, input.filters))
872870
throw new OrchestrationError('not_found', 'Document is outside the selected search filters')

0 commit comments

Comments
 (0)