Skip to content

Commit 9c3477d

Browse files
authored
fix(errors): actionable table read timeouts, 409 for duplicate custom tools, Instagram revocation, selector failure logs (#9880)
* fix(errors): actionable table read timeouts, 409 for duplicate custom tools, Instagram revocation, selector failure logs * fix(errors): address review on table timeout, selector logging, and route test context * fix(table): limit timeout guidance to the structured row query
1 parent a52e2f6 commit 9c3477d

11 files changed

Lines changed: 174 additions & 9 deletions

File tree

‎apps/sim/app/api/tools/custom/route.test.ts‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import {
55
authMockFns,
6+
createRouteContext,
67
hybridAuthMockFns,
78
permissionsMock,
89
permissionsMockFns,
@@ -15,8 +16,9 @@ import {
1516
import { NextRequest } from 'next/server'
1617
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
1718

18-
const { mockUpsertCustomTools } = vi.hoisted(() => ({
19+
const { mockUpsertCustomTools, MockCustomToolTitleConflictError } = vi.hoisted(() => ({
1920
mockUpsertCustomTools: vi.fn(),
21+
MockCustomToolTitleConflictError: class extends Error {},
2022
}))
2123

2224
const mockGetUserEntityPermissions = permissionsMockFns.mockGetUserEntityPermissions
@@ -82,11 +84,12 @@ vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock)
8284

8385
vi.mock('@/lib/workflows/custom-tools/operations', () => ({
8486
upsertCustomTools: (...args: unknown[]) => mockUpsertCustomTools(...args),
87+
CustomToolTitleConflictError: MockCustomToolTitleConflictError,
8588
}))
8689

8790
vi.mock('@/lib/workflows/utils', () => workflowsUtilsMock)
8891

89-
import { DELETE } from '@/app/api/tools/custom/route'
92+
import { DELETE, POST } from '@/app/api/tools/custom/route'
9093

9194
describe('Custom Tools API Routes', () => {
9295
const mockSession = { user: { id: 'user-123' } }
@@ -113,6 +116,41 @@ describe('Custom Tools API Routes', () => {
113116
resetDbChainMock()
114117
})
115118

119+
describe('POST /api/tools/custom', () => {
120+
function saveRequest() {
121+
return new NextRequest('http://localhost:3000/api/tools/custom', {
122+
method: 'POST',
123+
body: JSON.stringify({
124+
workspaceId: 'workspace-123',
125+
tools: [{ title: sampleTools[0].title, schema: sampleTools[0].schema, code: 'return 1' }],
126+
}),
127+
})
128+
}
129+
130+
it('answers a title the workspace already holds with 409, not 500', async () => {
131+
mockUpsertCustomTools.mockRejectedValueOnce(
132+
new MockCustomToolTitleConflictError('A tool with that title already exists')
133+
)
134+
135+
const response = await POST(saveRequest(), createRouteContext({}))
136+
137+
expect(response.status).toBe(409)
138+
expect(await response.json()).toEqual({ error: 'A tool with that title already exists' })
139+
})
140+
141+
it('answers a unique violation from a concurrent save with 409', async () => {
142+
mockUpsertCustomTools.mockRejectedValueOnce(
143+
Object.assign(new Error('duplicate key value violates unique constraint'), {
144+
code: '23505',
145+
})
146+
)
147+
148+
const response = await POST(saveRequest(), createRouteContext({}))
149+
150+
expect(response.status).toBe(409)
151+
})
152+
})
153+
116154
/**
117155
* Test DELETE endpoint
118156
*/

‎apps/sim/app/api/tools/custom/route.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { db } from '@sim/db'
33
import { customTools } from '@sim/db/schema'
44
import { createLogger } from '@sim/logger'
55
import { authorizeWorkflowByWorkspacePermission } from '@sim/platform-authz/workflow'
6-
import { getErrorMessage } from '@sim/utils/errors'
6+
import { getErrorMessage, getPostgresErrorCode } from '@sim/utils/errors'
77
import { and, desc, eq, isNull, or } from 'drizzle-orm'
88
import { type NextRequest, NextResponse } from 'next/server'
99
import {
@@ -16,7 +16,10 @@ import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
1616
import { generateRequestId } from '@/lib/core/utils/request'
1717
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1818
import { captureServerEvent } from '@/lib/posthog/server'
19-
import { upsertCustomTools } from '@/lib/workflows/custom-tools/operations'
19+
import {
20+
CustomToolTitleConflictError,
21+
upsertCustomTools,
22+
} from '@/lib/workflows/custom-tools/operations'
2023
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
2124

2225
const logger = createLogger('CustomToolsAPI')
@@ -176,6 +179,15 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
176179

177180
return NextResponse.json({ success: true, data: resultTools })
178181
} catch (error) {
182+
if (error instanceof CustomToolTitleConflictError) {
183+
return NextResponse.json({ error: error.message }, { status: 409 })
184+
}
185+
if (getPostgresErrorCode(error) === '23505') {
186+
return NextResponse.json(
187+
{ error: 'A custom tool with that title already exists in this workspace' },
188+
{ status: 409 }
189+
)
190+
}
179191
logger.error('Error updating custom tools', error)
180192
const errorMessage = getErrorMessage(error, 'Failed to update custom tools')
181193
return NextResponse.json({ error: errorMessage }, { status: 500 })

‎apps/sim/lib/oauth/refresh-error-codes.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ const APP_CONFIGURATION_ERRORS = new Set<string>([
2626
* Credential revocation codes only for the providers listed. Atlassian rejects a revoked or
2727
* rotated-out refresh token with `unauthorized_client`; elsewhere that code usually describes the
2828
* app registration, and treating it as terminal would send every credential of the provider to
29-
* reauthorization over one configuration fault.
29+
* reauthorization over one configuration fault. Instagram's Graph API rejects a refresh of a token
30+
* its user logged out of, revoked, or let expire with `OAuthException` code `190`.
3031
*/
3132
const PROVIDER_CREDENTIAL_REVOCATION_ERRORS: ReadonlyMap<string, ReadonlySet<string>> = new Map([
3233
['confluence', new Set(['unauthorized_client'])],
34+
['instagram', new Set(['190'])],
3335
['jira', new Set(['unauthorized_client'])],
3436
])
3537

‎apps/sim/lib/oauth/terminal-errors.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ describe('isCredentialRevocationError', () => {
6868
expect(isCredentialRevocationError('unauthorized_client', providerId)).toBe(false)
6969
}
7070
)
71+
72+
it.each([undefined, 'facebook', 'microsoft'])(
73+
"treats Meta's code 190 as a revocation only for Instagram, not %s",
74+
(providerId) => {
75+
expect(isCredentialRevocationError('190', 'instagram')).toBe(true)
76+
expect(isCredentialRevocationError('190', providerId)).toBe(false)
77+
}
78+
)
7179
})
7280

7381
describe('markCredentialDead / getRecentTerminalError / clearDeadFlag', () => {

‎apps/sim/lib/selectors/server/providers/http.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { createLogger } from '@sim/logger'
12
import { parseRetryAfter } from '@sim/utils/retry'
23
import {
34
SelectorConnectionUnavailableError,
45
SelectorOptionsUnavailableError,
56
} from '@/lib/selectors/server/errors'
67

8+
const logger = createLogger('SelectorProviderHttp')
9+
710
const PROVIDER_TIMEOUT_MS = 30_000
811
const MAX_PROVIDER_RESPONSE_BYTES = 16 * 1024 * 1024
912

@@ -44,6 +47,35 @@ async function readBoundedBody(response: Response): Promise<string> {
4447
return new TextDecoder().decode(body)
4548
}
4649

50+
/**
51+
* Host and path of a provider request. The query string is dropped and OData-style path
52+
* arguments (`search(q='...')`) are blanked, since both can carry tokens or a user's search text.
53+
*/
54+
function providerEndpoint(input: RequestInfo | URL): string | undefined {
55+
try {
56+
const url = new URL(input instanceof Request ? input.url : input)
57+
return `${url.host}${url.pathname.replace(/\([^/]*\)/g, '()')}`
58+
} catch {
59+
return undefined
60+
}
61+
}
62+
63+
/**
64+
* Records why a provider call failed before the error is reduced to a bare status, the only
65+
* place the upstream status and endpoint are still known.
66+
*/
67+
function logProviderFailure(
68+
input: RequestInfo | URL,
69+
reason: 'status' | 'network' | 'invalid_response',
70+
status?: number
71+
): void {
72+
logger.warn('Selector provider request failed', {
73+
endpoint: providerEndpoint(input),
74+
reason,
75+
...(status !== undefined ? { status } : {}),
76+
})
77+
}
78+
4779
export type ProviderJsonStatusResult<T> =
4880
| { ok: true; status: number; data: T }
4981
| { ok: false; status: number; retryAfterMs?: number }
@@ -86,6 +118,7 @@ export async function fetchProviderJsonWithStatus<T>(
86118
response = await fetch(input, { ...init, signal, redirect: init?.redirect ?? 'error' })
87119
} catch (error) {
88120
if (init?.signal?.aborted) throw error
121+
logProviderFailure(input, 'network')
89122
if (options.passthroughNetworkErrors) throw new RetryableProviderNetworkError()
90123
throw new SelectorOptionsUnavailableError()
91124
}
@@ -103,6 +136,7 @@ export async function fetchProviderJsonWithStatus<T>(
103136
...(retryAfterMs !== null && retryAfterMs > 0 ? { retryAfterMs } : {}),
104137
}
105138
}
139+
logProviderFailure(input, 'status', response.status)
106140
throw selectorProviderStatusError(response.status)
107141
}
108142
try {
@@ -113,6 +147,13 @@ export async function fetchProviderJsonWithStatus<T>(
113147
}
114148
} catch (error) {
115149
if (init?.signal?.aborted) throw error
150+
logProviderFailure(
151+
input,
152+
error instanceof SyntaxError || error instanceof SelectorOptionsUnavailableError
153+
? 'invalid_response'
154+
: 'network',
155+
response.status
156+
)
116157
if (error instanceof SelectorOptionsUnavailableError) throw error
117158
throw new SelectorOptionsUnavailableError()
118159
}

‎apps/sim/lib/table/application/rows.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,37 @@ describe('row query and upsert application semantics', () => {
445445
expect(mockQueryRows).not.toHaveBeenCalled()
446446
})
447447

448+
it('turns a read cancelled by its statement timeout into an actionable 400', async () => {
449+
mockQueryRows.mockRejectedValueOnce(
450+
Object.assign(new Error('canceling statement due to statement timeout'), { code: '57014' })
451+
)
452+
453+
await expect(
454+
queryTableRows.execute({
455+
principal: PRINCIPAL,
456+
input: { tableId: TABLE.id, limit: 100 },
457+
})
458+
).rejects.toMatchObject({
459+
code: 'validation',
460+
details: { code: 'TABLE_QUERY_TIMEOUT' },
461+
message: expect.stringContaining('Sort by createdAt'),
462+
})
463+
})
464+
465+
it('keeps a caller-cancelled read as the raw cancellation, not a timeout', async () => {
466+
const cancelled = Object.assign(new Error('canceling statement due to user request'), {
467+
code: '57014',
468+
})
469+
mockQueryRows.mockRejectedValueOnce(cancelled)
470+
471+
await expect(
472+
queryTableRows.execute({
473+
principal: PRINCIPAL,
474+
input: { tableId: TABLE.id, limit: 100 },
475+
})
476+
).rejects.toBe(cancelled)
477+
})
478+
448479
it('resumes the same offset page under the identical predicate', async () => {
449480
const cursor = encodeCursor({
450481
lastRow: { id: 'row-100', orderKey: null },

‎apps/sim/lib/table/application/rows.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { isDeepStrictEqual } from 'node:util'
22
import { AuditAction, AuditResourceType } from '@sim/audit'
33
import { type Principal, resolvePrincipalAttribution } from '@sim/auth/principal'
44
import { db } from '@sim/db'
5-
import { getRequestContext } from '@sim/logger'
5+
import { createLogger, getRequestContext } from '@sim/logger'
6+
import { getPostgresCancellationReason } from '@sim/utils/errors'
67
import { generateId } from '@sim/utils/id'
78
import { isPlainRecord, toRecord } from '@sim/utils/object'
89
import {
@@ -77,6 +78,7 @@ import { columnTypeOf } from '@/lib/table/column-types'
7778
import { TableQueryValidationError } from '@/lib/table/errors'
7879
import { signalTableRowsChanged, signalTableRowsChangedByActor } from '@/lib/table/events'
7980
import { CSV_MAX_BATCH_SIZE } from '@/lib/table/import'
81+
import { READ_STATEMENT_TIMEOUT_MS } from '@/lib/table/planner'
8082
import { isTablePredicate, predicateToFilter } from '@/lib/table/query-builder/converters'
8183
import {
8284
validatePredicate,
@@ -98,6 +100,8 @@ import { predicateToStorage, resolveFilterSelectValues } from '@/lib/table/selec
98100
import { coerceRowValues } from '@/lib/table/validation'
99101
import type { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
100102

103+
const logger = createLogger('TableRowsApplication')
104+
101105
const ROW_CHANGE_COUNTERS = ['rowsInserted', 'rowsUpdated', 'rowsDeleted'] as const
102106

103107
/**
@@ -501,6 +505,25 @@ async function throwValidationResponse(
501505
throw new TableRowsValidationError(body.error ?? 'Invalid row data', body.details)
502506
}
503507

508+
/**
509+
* A structured row query cancelled by its own `statement_timeout`: the query shape costs more than
510+
* the table's size allows. Sorting or range-filtering on a stored column reads every row's data, so it is the
511+
* caller's to narrow, and retrying the same query only times out again.
512+
*/
513+
function rethrowReadQueryError(error: unknown, tableId: string): never {
514+
if (getPostgresCancellationReason(error) === 'statement_timeout') {
515+
logger.warn('Table read hit its statement timeout', { tableId })
516+
throw new TableRowsValidationError(
517+
`The query took longer than ${READ_STATEMENT_TIMEOUT_MS / 1000} seconds and was stopped. ` +
518+
'Sorting or range-filtering on a column reads every row in the table, which is too slow ' +
519+
'at this table size. Sort by createdAt, add an equality filter, or keep ' +
520+
'fewer rows (an Expiration column deletes old rows automatically).',
521+
{ code: 'TABLE_QUERY_TIMEOUT' }
522+
)
523+
}
524+
rethrowQueryValidation(error)
525+
}
526+
504527
function rethrowQueryValidation(error: unknown): never {
505528
if (error instanceof TableQueryValidationError) {
506529
throw new TableRowsValidationError(error.message, error.code ? { code: error.code } : undefined)
@@ -680,7 +703,7 @@ export const queryTableRows = defineAuthorizedTableUseCase({
680703
: {}),
681704
}
682705
} catch (error) {
683-
rethrowQueryValidation(error)
706+
rethrowReadQueryError(error, context.table.id)
684707
}
685708
},
686709
})

‎apps/sim/lib/table/errors.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type TableQueryErrorCode =
99
| 'CURSOR_FILTER_CONFLICT'
1010
| 'INVALID_FILTER'
1111
| 'INVALID_ORDER'
12+
| 'TABLE_QUERY_TIMEOUT'
1213

1314
/**
1415
* Error thrown when caller-supplied filter or sort input is malformed.

‎apps/sim/lib/table/planner.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type DbTransaction = Parameters<Parameters<typeof db.transaction>[0]>[0]
1212
* small shared `web` pool — one abusive key can starve every tenant. `SET LOCAL`
1313
* scopes both to the surrounding transaction, so they die when it commits.
1414
*/
15-
const READ_STATEMENT_TIMEOUT_MS = 15_000
15+
export const READ_STATEMENT_TIMEOUT_MS = 15_000
1616
const READ_LOCK_TIMEOUT_MS = 3_000
1717

1818
/**

‎apps/sim/lib/workflows/custom-tools/operations.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ const logger = createLogger('CustomToolsOperations')
2323

2424
export type CustomToolSortBy = 'title' | 'createdAt' | 'updatedAt'
2525

26+
/** A strict batch write found a title the workspace already holds; the caller's to fix. */
27+
export class CustomToolTitleConflictError extends Error {
28+
constructor(title: string) {
29+
super(`A tool with the title "${title}" already exists in this workspace`)
30+
this.name = 'CustomToolTitleConflictError'
31+
}
32+
}
33+
2634
interface CustomToolWriteParams {
2735
tools: Array<{
2836
id?: string
@@ -144,7 +152,7 @@ async function writeCustomTools(
144152
.limit(1)
145153

146154
if (duplicateTitle.length > 0) {
147-
throw new Error(`A tool with the title "${tool.title}" already exists in this workspace`)
155+
throw new CustomToolTitleConflictError(tool.title)
148156
}
149157

150158
await tx.insert(customTools).values(row)

0 commit comments

Comments
 (0)