Skip to content

Commit 1dfa35d

Browse files
committed
refactor(utils): replace inline toError, isRecordLike, omit, and truncate idioms with @sim/utils helpers
1 parent 9dc14f0 commit 1dfa35d

21 files changed

Lines changed: 52 additions & 53 deletions

File tree

‎apps/desktop/src/main/local-filesystem.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from '@sim/desktop-bridge/local-filesystem-limits'
2020
import { generateId } from '@sim/utils/id'
2121
import { isRecordLike } from '@sim/utils/object'
22-
import { escapeRegExp } from '@sim/utils/string'
22+
import { escapeRegExp, truncate } from '@sim/utils/string'
2323
import { app, dialog, shell } from 'electron'
2424
import micromatch from 'micromatch'
2525
import safeRegex from 'safe-regex2'
@@ -1236,8 +1236,7 @@ export class LocalFilesystemService {
12361236
matches.push({
12371237
uri: resultUri,
12381238
line: request.lineNumbers === false ? 0 : contextIndex + 1,
1239-
text:
1240-
line.length > MAX_GREP_LINE_LENGTH ? `${line.slice(0, MAX_GREP_LINE_LENGTH)}…` : line,
1239+
text: truncate(line, MAX_GREP_LINE_LENGTH, '…'),
12411240
})
12421241
if (matches.length >= maxResults) {
12431242
truncated = true

‎apps/sim/app/api/mcp/oauth/start/route.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { db } from '@sim/db'
33
import { mcpServers } from '@sim/db/schema'
44
import { createLogger } from '@sim/logger'
55
import { getErrorMessage, toError } from '@sim/utils/errors'
6+
import { truncate } from '@sim/utils/string'
67
import { and, eq, isNull } from 'drizzle-orm'
78
import type { NextRequest } from 'next/server'
89
import { NextResponse } from 'next/server'
@@ -61,7 +62,7 @@ function isDynamicClientRegistrationUnsupported(error: unknown): boolean {
6162
export function surfaceOauthError(error: unknown): string {
6263
// Spec-compliant OAuth servers throw typed subclasses with clean RFC 6749 fields.
6364
if (error instanceof OAuthError && !(error instanceof ServerError)) {
64-
return truncate(`${error.errorCode}: ${error.message}`)
65+
return truncateSurfacedError(`${error.errorCode}: ${error.message}`)
6566
}
6667

6768
// ServerError wraps non-spec response bodies as "HTTP N: Invalid OAuth error
@@ -76,18 +77,16 @@ export function surfaceOauthError(error: unknown): string {
7677
(typeof body.message === 'string' && body.message) ||
7778
(typeof body.error === 'string' && body.error) ||
7879
null
79-
if (vendorMessage) return truncate(`Authorization server: ${vendorMessage}`)
80+
if (vendorMessage) return truncateSurfacedError(`Authorization server: ${vendorMessage}`)
8081
} catch {}
8182
}
82-
return truncate(error.message.split('\n')[0] || 'Failed to start OAuth flow')
83+
return truncateSurfacedError(error.message.split('\n')[0] || 'Failed to start OAuth flow')
8384
}
8485
return 'Failed to start OAuth flow'
8586
}
8687

87-
function truncate(message: string): string {
88-
return message.length > MAX_SURFACED_ERROR_LENGTH
89-
? `${message.slice(0, MAX_SURFACED_ERROR_LENGTH)}…`
90-
: message
88+
function truncateSurfacedError(message: string): string {
89+
return truncate(message, MAX_SURFACED_ERROR_LENGTH, '…')
9190
}
9291

9392
export const dynamic = 'force-dynamic'

‎apps/sim/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/tool-permission-card.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
toast,
1515
} from '@sim/emcn'
1616
import { createLogger } from '@sim/logger'
17+
import { truncate } from '@sim/utils/string'
1718
import { useQueryClient } from '@tanstack/react-query'
1819
import {
1920
ACTIVITY_ICON_SLOT_CLASS,
@@ -60,7 +61,7 @@ function argsPreview(params: Record<string, unknown> | undefined): string | unde
6061
if (!params || Object.keys(params).length === 0) return undefined
6162
try {
6263
const json = JSON.stringify(params, null, 2)
63-
return json.length > 600 ? `${json.slice(0, 600)}…` : json
64+
return truncate(json, 600, '…')
6465
} catch {
6566
return undefined
6667
}

‎apps/sim/blocks/blocks/google_slides.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { omit } from '@sim/utils/object'
12
import { GoogleSlidesIcon } from '@/components/icons'
23
import { getScopesForService } from '@/lib/oauth/utils'
34
import { resolveHttpsUrlFromFileInput } from '@/lib/uploads/utils/file-utils'
@@ -3552,9 +3553,7 @@ const googleSlidesV2SubBlocks = (GoogleSlidesBlock.subBlocks || []).flatMap((sub
35523553

35533554
const googleSlidesV2Inputs = GoogleSlidesBlock.inputs
35543555
? {
3555-
...Object.fromEntries(
3556-
Object.entries(GoogleSlidesBlock.inputs).filter(([key]) => key !== 'imageSource')
3557-
),
3556+
...omit(GoogleSlidesBlock.inputs, ['imageSource']),
35583557
imageFile: { type: 'json', description: 'Image source (file or URL)' },
35593558
}
35603559
: {}

‎apps/sim/connectors/airtable/airtable.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createLogger } from '@sim/logger'
22
import { getErrorMessage, toError } from '@sim/utils/errors'
3+
import { truncate } from '@sim/utils/string'
34
import { fetchWithRetry } from '@/lib/knowledge/documents/secure-fetch.server'
45
import { VALIDATE_RETRY_OPTIONS } from '@/lib/knowledge/documents/utils'
56
import { airtableConnectorMeta } from '@/connectors/airtable/meta'
@@ -98,7 +99,7 @@ function renderTitle(value: unknown): string | null {
9899
if (value == null) return null
99100
const rendered = typeof value === 'object' ? formatCellValue(value).trim() : String(value).trim()
100101
if (!rendered) return null
101-
return rendered.length > MAX_TITLE_LENGTH ? `${rendered.slice(0, MAX_TITLE_LENGTH)}…` : rendered
102+
return truncate(rendered, MAX_TITLE_LENGTH, '…')
102103
}
103104

104105
/**

‎apps/sim/executor/execution/block-executor.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLogger, type Logger } from '@sim/logger'
2-
import { describeError } from '@sim/utils/errors'
2+
import { describeError, toError } from '@sim/utils/errors'
33
import { sleep } from '@sim/utils/helpers'
44
import { isRecordLike, toRecord } from '@sim/utils/object'
55
import { DrizzleQueryError } from 'drizzle-orm/errors'
@@ -1264,7 +1264,7 @@ export class BlockExecutor {
12641264
if (onStreamPromise) {
12651265
await onStreamPromise.catch(() => {})
12661266
}
1267-
throw error instanceof Error ? error : new Error(String(error))
1267+
throw toError(error)
12681268
}
12691269

12701270
if (onStreamPromise) {

‎apps/sim/executor/handlers/pi/cloud/authoring/backend.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ function mergeChangedFiles(
322322
*/
323323
function mergePhaseDiffs(createDiff: string | undefined, babysitDiff: string | undefined): string {
324324
const merged = [createDiff, babysitDiff].filter((diff): diff is string => !!diff).join('\n')
325-
return merged.length > MAX_DIFF_BYTES
326-
? `${merged.slice(0, MAX_DIFF_BYTES)}\n[diff truncated]`
327-
: merged
325+
return truncate(merged, MAX_DIFF_BYTES, '\n[diff truncated]')
328326
}
329327

330328
function combineAuthoringAndBabysit(
@@ -554,8 +552,7 @@ async function runCloudAuthoringPi(
554552
let diff: string | undefined
555553
try {
556554
const raw = await runner.readFile(DIFF_PATH)
557-
diff =
558-
raw.length > MAX_DIFF_BYTES ? `${raw.slice(0, MAX_DIFF_BYTES)}\n[diff truncated]` : raw
555+
diff = truncate(raw, MAX_DIFF_BYTES, '\n[diff truncated]')
559556
} catch {
560557
diff = undefined
561558
}

‎apps/sim/executor/handlers/pi/cloud/babysit/backend.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function isQuotedGitPath(path: string): boolean {
297297
* the net guard alone let the reported `diff` grow to megabytes across rounds.
298298
*/
299299
function capDiff(text: string): string {
300-
return text.length > MAX_DIFF_BYTES ? `${text.slice(0, MAX_DIFF_BYTES)}\n[diff truncated]` : text
300+
return truncate(text, MAX_DIFF_BYTES, '\n[diff truncated]')
301301
}
302302

303303
function mergeRoundTotals(total: PiRunTotals, round: PiRunTotals): void {

‎apps/sim/executor/handlers/pi/local/ssh-tools.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { createLogger } from '@sim/logger'
99
import { getErrorMessage } from '@sim/utils/errors'
10+
import { truncate } from '@sim/utils/string'
1011
import type { Client, SFTPWrapper } from 'ssh2'
1112
import {
1213
createSSHConnection,
@@ -129,7 +130,7 @@ export async function captureRepoChanges(
129130
}
130131
const raw = await executeSSHCommand(session.client, `${scoped} && git diff HEAD`)
131132
const out = raw.exitCode === 0 ? raw.stdout : ''
132-
const diff = out.length > maxDiffBytes ? `${out.slice(0, maxDiffBytes)}\n[diff truncated]` : out
133+
const diff = truncate(out, maxDiffBytes, '\n[diff truncated]')
133134
return { changedFiles: [...fileSet], diff }
134135
} catch {
135136
return { changedFiles: [], diff: '' }

‎apps/sim/executor/variables/resolver.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ export class VariableResolver {
714714
)
715715
return replacement
716716
} catch (error) {
717-
replacementError = error instanceof Error ? error : new Error(String(error))
717+
replacementError = toError(error)
718718
displayResult += match
719719
return match
720720
}

0 commit comments

Comments
 (0)