Skip to content

Commit 7d1f26f

Browse files
committed
Refactor to add auth error handling
1 parent fc3f2aa commit 7d1f26f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/commands/dependencies.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { printFlagList } from '../utils/formatting'
1313
import { getDefaultKey, setupSdk } from '../utils/sdk'
1414

1515
import type { CliSubcommand } from '../utils/meow-with-subcommands'
16-
import type { Ora } from 'ora'
16+
import { AuthError } from '../utils/errors'
1717

1818
export const dependencies: CliSubcommand = {
1919
description:
@@ -23,9 +23,7 @@ export const dependencies: CliSubcommand = {
2323

2424
const input = setupCommand(name, dependencies.description, argv, importMeta)
2525
if (input) {
26-
const spinnerText = 'Searching dependencies...'
27-
const spinner = ora(spinnerText).start()
28-
await searchDeps(input, spinner)
26+
await searchDeps(input)
2927
}
3028
}
3129
}
@@ -100,10 +98,17 @@ function setupCommand(
10098
}
10199

102100
async function searchDeps(
103-
{ limit, offset, outputJson }: CommandContext,
104-
spinner: Ora
101+
{ limit, offset, outputJson }: CommandContext
105102
): Promise<void> {
106-
const socketSdk = await setupSdk(getDefaultKey())
103+
const apiKey = getDefaultKey()
104+
if(!apiKey){
105+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
106+
}
107+
const spinnerText = 'Searching dependencies...'
108+
const spinner = ora(spinnerText).start()
109+
110+
const socketSdk = await setupSdk(apiKey)
111+
107112
const result = await handleApiCall(
108113
socketSdk.searchDependencies({ limit, offset }),
109114
'Searching dependencies'

0 commit comments

Comments
 (0)