Skip to content

Commit 874325e

Browse files
committed
chore(cli): update try catch error with unknown
1 parent ac26981 commit 874325e

8 files changed

Lines changed: 85 additions & 59 deletions

File tree

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true

packages/cli/src/commands/agent.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type AgentType,
1616
} from '@ai-devkit/agent-manager';
1717
import { ui } from '../util/terminal-ui';
18+
import { getErrorMessage } from '../util/text';
1819

1920
const STATUS_DISPLAY: Record<AgentStatus, { emoji: string; label: string }> = {
2021
[AgentStatus.RUNNING]: { emoji: '🟢', label: 'run' },
@@ -134,8 +135,8 @@ export function registerAgentCommand(program: Command): void {
134135
ui.warning(`${waitingCount} agent(s) waiting for input.`);
135136
}
136137

137-
} catch (error: any) {
138-
ui.error(`Failed to list agents: ${error.message}`);
138+
} catch (error: unknown) {
139+
ui.error(`Failed to list agents: ${getErrorMessage(error)}`);
139140
process.exit(1);
140141
}
141142
});
@@ -210,8 +211,8 @@ export function registerAgentCommand(program: Command): void {
210211
spinner.fail(`Failed to switch focus to ${agent.name}.`);
211212
}
212213

213-
} catch (error: any) {
214-
ui.error(`Failed to open agent: ${error.message}`);
214+
} catch (error: unknown) {
215+
ui.error(`Failed to open agent: ${getErrorMessage(error)}`);
215216
process.exit(1);
216217
}
217218
});
@@ -265,8 +266,8 @@ export function registerAgentCommand(program: Command): void {
265266
await TtyWriter.send(location, message);
266267
ui.success(`Sent message to ${agent.name}.`);
267268

268-
} catch (error: any) {
269-
ui.error(`Failed to send message: ${error.message}`);
269+
} catch (error: unknown) {
270+
ui.error(`Failed to send message: ${getErrorMessage(error)}`);
270271
process.exit(1);
271272
}
272273
});
@@ -397,8 +398,8 @@ export function registerAgentCommand(program: Command): void {
397398
ui.info(`Showing last ${displayMessages.length} of ${conversation.length} messages. Use --full to see all.`);
398399
}
399400

400-
} catch (error: any) {
401-
ui.error(`Failed to get agent detail: ${error.message}`);
401+
} catch (error: unknown) {
402+
ui.error(`Failed to get agent detail: ${getErrorMessage(error)}`);
402403
process.exit(1);
403404
}
404405
});

packages/cli/src/commands/channel.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type TelegramConfig,
2323
} from '@ai-devkit/channel-connector';
2424
import { ui } from '../util/terminal-ui';
25+
import { getErrorMessage } from '../util/text';
2526
import { createLogger, enableDebug } from '../util/debug';
2627

2728
const debug = createLogger('channel');
@@ -98,9 +99,10 @@ function setupInputHandler(
9899
try {
99100
await TtyWriter.send(terminalLocation, msg.text);
100101
debug(`Sent message to agent terminal (length: ${msg.text?.length ?? 0})`);
101-
} catch (error: any) {
102-
ui.error(`Failed to send to agent: ${error.message}`);
103-
await telegram.sendMessage(msg.chatId, `Failed to send to agent: ${error.message}`);
102+
} catch (error: unknown) {
103+
const message = getErrorMessage(error);
104+
ui.error(`Failed to send to agent: ${message}`);
105+
await telegram.sendMessage(msg.chatId, `Failed to send to agent: ${message}`);
104106
}
105107
});
106108
}
@@ -214,7 +216,7 @@ export function registerChannelCommand(program: Command): void {
214216
const me = await bot.telegram.getMe();
215217
botUsername = me.username;
216218
spinner.succeed(`Connected to bot @${botUsername}`);
217-
} catch (error: any) {
219+
} catch (error: unknown) {
218220
spinner.fail('Invalid bot token. Please check and try again.');
219221
return;
220222
}
@@ -234,8 +236,8 @@ export function registerChannelCommand(program: Command): void {
234236
ui.info(`Bot: @${botUsername}`);
235237
ui.info('Run "ai-devkit channel start --agent <name>" to start the bridge.');
236238

237-
} catch (error: any) {
238-
ui.error(`Failed to connect channel: ${error.message}`);
239+
} catch (error: unknown) {
240+
ui.error(`Failed to connect channel: ${getErrorMessage(error)}`);
239241
process.exit(1);
240242
}
241243
});
@@ -272,8 +274,8 @@ export function registerChannelCommand(program: Command): void {
272274
rows,
273275
});
274276

275-
} catch (error: any) {
276-
ui.error(`Failed to list channels: ${error.message}`);
277+
} catch (error: unknown) {
278+
ui.error(`Failed to list channels: ${getErrorMessage(error)}`);
277279
process.exit(1);
278280
}
279281
});
@@ -303,8 +305,8 @@ export function registerChannelCommand(program: Command): void {
303305
await configStore.removeChannel(type);
304306
ui.success(`${type} channel disconnected.`);
305307

306-
} catch (error: any) {
307-
ui.error(`Failed to disconnect channel: ${error.message}`);
308+
} catch (error: unknown) {
309+
ui.error(`Failed to disconnect channel: ${getErrorMessage(error)}`);
308310
process.exit(1);
309311
}
310312
});
@@ -387,9 +389,10 @@ export function registerChannelCommand(program: Command): void {
387389

388390
// Keep process running
389391
await new Promise(() => {});
390-
} catch (error: any) {
391-
debug(`Error caught: ${error.stack ?? error.message}`);
392-
ui.error(`Failed to start channel bridge: ${error.message}`);
392+
} catch (error: unknown) {
393+
const message = getErrorMessage(error);
394+
debug(`Error caught: ${error instanceof Error ? error.stack : message}`);
395+
ui.error(`Failed to start channel bridge: ${message}`);
393396
process.exit(1);
394397
}
395398
});

packages/cli/src/commands/skill.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from 'chalk';
33
import { ConfigManager } from '../lib/Config';
44
import { SkillManager } from '../lib/SkillManager';
55
import { ui } from '../util/terminal-ui';
6-
import { truncate } from '../util/text';
6+
import { truncate, getErrorMessage } from '../util/text';
77

88
export function registerSkillCommand(program: Command): void {
99
const skillCommand = program
@@ -24,12 +24,13 @@ export function registerSkillCommand(program: Command): void {
2424
global: options.global,
2525
environments: options.env,
2626
});
27-
} catch (error: any) {
28-
if (error.message === 'Skill selection cancelled.') {
27+
} catch (error: unknown) {
28+
const message = getErrorMessage(error);
29+
if (message === 'Skill selection cancelled.') {
2930
ui.warning('Skill selection cancelled.');
3031
return;
3132
}
32-
ui.error(`Failed to add skill: ${error.message}`);
33+
ui.error(`Failed to add skill: ${message}`);
3334
process.exit(1);
3435
}
3536
});
@@ -63,8 +64,8 @@ export function registerSkillCommand(program: Command): void {
6364
});
6465

6566
ui.text(`Total: ${skills.length} skill(s)`, { breakline: true });
66-
} catch (error: any) {
67-
ui.error(`Failed to list skills: ${error.message}`);
67+
} catch (error: unknown) {
68+
ui.error(`Failed to list skills: ${getErrorMessage(error)}`);
6869
process.exit(1);
6970
}
7071
});
@@ -78,8 +79,8 @@ export function registerSkillCommand(program: Command): void {
7879
const skillManager = new SkillManager(configManager);
7980

8081
await skillManager.removeSkill(skillName);
81-
} catch (error: any) {
82-
ui.error(`Failed to remove skill: ${error.message}`);
82+
} catch (error: unknown) {
83+
ui.error(`Failed to remove skill: ${getErrorMessage(error)}`);
8384
process.exit(1);
8485
}
8586
});
@@ -93,8 +94,8 @@ export function registerSkillCommand(program: Command): void {
9394
const skillManager = new SkillManager(configManager);
9495

9596
await skillManager.updateSkills(registryId);
96-
} catch (error: any) {
97-
ui.error(`Failed to update skills: ${error.message}`);
97+
} catch (error: unknown) {
98+
ui.error(`Failed to update skills: ${getErrorMessage(error)}`);
9899
process.exit(1);
99100
}
100101
});
@@ -129,8 +130,8 @@ export function registerSkillCommand(program: Command): void {
129130
});
130131

131132
ui.text(`\nInstall with: ai-devkit skill add <registry> [skill-name]`, { breakline: true });
132-
} catch (error: any) {
133-
ui.error(`Failed to search skills: ${error.message}`);
133+
} catch (error: unknown) {
134+
ui.error(`Failed to search skills: ${getErrorMessage(error)}`);
134135
process.exit(1);
135136
}
136137
});
@@ -145,8 +146,8 @@ export function registerSkillCommand(program: Command): void {
145146
const skillManager = new SkillManager(configManager);
146147

147148
await skillManager.rebuildIndex(options.output);
148-
} catch (error: any) {
149-
ui.error(`Failed to rebuild index: ${error.message}`);
149+
} catch (error: unknown) {
150+
ui.error(`Failed to rebuild index: ${getErrorMessage(error)}`);
150151
process.exit(1);
151152
}
152153
});

packages/cli/src/lib/SkillIndex.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { extractSkillDescription } from '../util/skill';
66
import { fetchGitHead } from '../util/git';
77
import { fetchGitHubSkillPaths, fetchRawGitHubFile } from '../util/github';
88
import { ui } from '../util/terminal-ui';
9+
import { getErrorMessage } from '../util/text';
910

1011
const SEED_INDEX_URL = 'https://raw.githubusercontent.com/codeaholicguy/ai-devkit/main/skills/index.json';
1112
const SKILL_INDEX_PATH = path.join(os.homedir(), '.ai-devkit', 'skills.json');
@@ -59,9 +60,9 @@ export class SkillIndex {
5960
await fs.writeJson(targetPath, newIndex, { spaces: 2 });
6061
spinner.succeed(`Skill index rebuilt: ${newIndex.skills.length} skills`);
6162
ui.info(`Written to: ${targetPath}`);
62-
} catch (error: any) {
63+
} catch (error: unknown) {
6364
spinner.fail('Failed to rebuild index');
64-
throw new Error(`Failed to rebuild skill index: ${error.message}`);
65+
throw new Error(`Failed to rebuild skill index: ${getErrorMessage(error)}`);
6566
}
6667
}
6768

@@ -108,15 +109,15 @@ export class SkillIndex {
108109
await fs.writeJson(SKILL_INDEX_PATH, newIndex, { spaces: 2 });
109110
spinner.succeed('Skill index updated');
110111
return newIndex;
111-
} catch (error: any) {
112+
} catch (error: unknown) {
112113
spinner.fail('Failed to build index');
113114

114115
if (!forceRefresh && await fs.pathExists(SKILL_INDEX_PATH)) {
115116
ui.warning('Using stale index due to error');
116117
return await fs.readJson(SKILL_INDEX_PATH);
117118
}
118119

119-
throw new Error(`Failed to build skill index: ${error.message}`);
120+
throw new Error(`Failed to build skill index: ${getErrorMessage(error)}`);
120121
}
121122
}
122123

packages/cli/src/lib/SkillManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,9 @@ export class SkillManager {
420420
]);
421421

422422
return selectedSkills;
423-
} catch (error: any) {
424-
if (error?.name === 'ExitPromptError' || error?.message?.toLowerCase().includes('cancel')) {
423+
} catch (error: unknown) {
424+
if (error instanceof Error &&
425+
(error.name === 'ExitPromptError' || error.message.toLowerCase().includes('cancel'))) {
425426
throw new Error('Skill selection cancelled.');
426427
}
427428

packages/cli/src/lib/SkillRegistry.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ConfigManager } from './Config';
55
import { GlobalConfigManager } from './GlobalConfig';
66
import { ensureGitInstalled, cloneRepository, isGitRepository, pullRepository } from '../util/git';
77
import { ui } from '../util/terminal-ui';
8+
import { getErrorMessage } from '../util/text';
89

910
export const REGISTRY_URL = 'https://raw.githubusercontent.com/codeaholicguy/ai-devkit/main/skills/registry.json';
1011
export const SKILL_CACHE_DIR = path.join(os.homedir(), '.ai-devkit', 'skills');
@@ -50,8 +51,8 @@ export class SkillRegistry {
5051
try {
5152
const defaultRegistry = await this.fetchDefaultRegistry();
5253
defaultRegistries = defaultRegistry.registries || {};
53-
} catch (error: any) {
54-
ui.warning(`Failed to fetch default registry: ${error.message}`);
54+
} catch (error: unknown) {
55+
ui.warning(`Failed to fetch default registry: ${getErrorMessage(error)}`);
5556
defaultRegistries = {};
5657
}
5758

@@ -99,9 +100,9 @@ export class SkillRegistry {
99100

100101
try {
101102
return await this.cloneRepositoryToCache(registryId, gitUrl);
102-
} catch (error: any) {
103+
} catch (error: unknown) {
103104
if (await fs.pathExists(cachedPath)) {
104-
ui.warning(`Failed to refresh ${registryId}: ${error.message}. Using cached registry contents.`);
105+
ui.warning(`Failed to refresh ${registryId}: ${getErrorMessage(error)}. Using cached registry contents.`);
105106
return cachedPath;
106107
}
107108

@@ -194,12 +195,12 @@ export class SkillRegistry {
194195
status: 'success',
195196
message: 'Updated successfully',
196197
};
197-
} catch (error: any) {
198+
} catch (error: unknown) {
198199
return {
199200
registryId,
200201
status: 'error',
201-
message: error.message,
202-
error,
202+
message: getErrorMessage(error),
203+
error: error instanceof Error ? error : new Error(String(error)),
203204
};
204205
}
205206
}

packages/cli/src/util/text.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
export function truncate(text: string, maxLength: number, replaceText: string = '...'): string {
2-
if (maxLength <= 0) {
3-
return '';
4-
}
1+
export function getErrorMessage(error: unknown): string {
2+
if (error instanceof Error) return error.message;
3+
return String(error);
4+
}
5+
6+
export function truncate(
7+
text: string,
8+
maxLength: number,
9+
replaceText: string = "...",
10+
): string {
11+
if (maxLength <= 0) {
12+
return "";
13+
}
514

6-
if (text.length <= maxLength) {
7-
return text;
8-
}
15+
if (text.length <= maxLength) {
16+
return text;
17+
}
918

10-
if (replaceText.length >= maxLength) {
11-
return replaceText.substring(0, maxLength);
12-
}
19+
if (replaceText.length >= maxLength) {
20+
return replaceText.substring(0, maxLength);
21+
}
1322

14-
return `${text.substring(0, maxLength - replaceText.length)}${replaceText}`;
23+
return `${text.substring(0, maxLength - replaceText.length)}${replaceText}`;
1524
}

0 commit comments

Comments
 (0)