Skip to content

Commit 1623f52

Browse files
committed
Use CResult in findSocketYmlSync
1 parent 63d8a66 commit 1623f52

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/utils/alerts-map.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getPublicApiToken, setupSdk } from './sdk.mts'
1111
import { addArtifactToAlertsMap } from './socket-package-alert.mts'
1212

1313
import type { CompactSocketArtifact } from './alert/artifact.mts'
14+
import type { FoundSocketYml } from './config.mts'
1415
import type { AlertFilter, AlertsByPurl } from './socket-package-alert.mts'
1516
import type { LockfileObject } from '@pnpm/lockfile.fs'
1617
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
@@ -83,7 +84,10 @@ export async function getAlertsMapFromPurls(
8384
throw new Error('Auth error: Run `socket login` first')
8485
}
8586
const sockSdk = sockSdkCResult.data
86-
const socketYml = findSocketYmlSync()?.parsed
87+
const socketYmlResult = findSocketYmlSync()
88+
const socketYml = socketYmlResult.ok
89+
? (socketYmlResult.data as FoundSocketYml).parsed
90+
: undefined
8791

8892
const alertsMapOptions = {
8993
consolidate: opts.consolidate,

src/utils/config.mts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export type FoundSocketYml = {
111111

112112
export function findSocketYmlSync(
113113
dir = process.cwd(),
114-
): FoundSocketYml | undefined {
114+
): CResult<FoundSocketYml | undefined> {
115115
let prevDir = null
116116
while (dir !== prevDir) {
117117
let ymlPath = path.join(dir, 'socket.yml')
@@ -123,18 +123,25 @@ export function findSocketYmlSync(
123123
if (typeof yml === 'string') {
124124
try {
125125
return {
126-
path: ymlPath,
127-
parsed: config.parseSocketConfig(yml),
126+
ok: true,
127+
data: {
128+
path: ymlPath,
129+
parsed: config.parseSocketConfig(yml),
130+
},
128131
}
129132
} catch (e) {
130133
debugDir('inspect', { error: e })
131-
throw new Error(`Found file but was unable to parse ${ymlPath}`)
134+
return {
135+
ok: false,
136+
message: `Found file but was unable to parse ${ymlPath}`,
137+
cause: e instanceof Error ? e.message : String(e),
138+
}
132139
}
133140
}
134141
prevDir = dir
135142
dir = path.join(dir, '..')
136143
}
137-
return undefined
144+
return { ok: true, data: undefined }
138145
}
139146

140147
export function getConfigValue<Key extends keyof LocalConfig>(

0 commit comments

Comments
 (0)