fix(api): add try-catch error handling to route handlers

- Add try-catch to cliproxy-auth-routes GET /
- Add try-catch to health-routes GET / and POST /fix/:checkId
- Add try-catch to config-routes GET /format and POST /migrate
- Improve error messages with specific context
- All handlers now return 500 with error message on failure
This commit is contained in:
kaitranntt committed 2025-12-21 18:03:37 -05:00
1 parent 557926ffe3
commit 85b0f17110
3 files changed
+50 -6

No files matched your search

+31 -3
View File
@@ -32,7 +32,8 @@ const validProviders: CLIProxyProvider[] = ['gemini', 'codex', 'agy', 'qwen', 'i
* GET /api/cliproxy/auth - Get auth status for built-in CLIProxy profiles * GET /api/cliproxy/auth - Get auth status for built-in CLIProxy profiles
* Also fetches CLIProxyAPI stats to update lastUsedAt for active providers * Also fetches CLIProxyAPI stats to update lastUsedAt for active providers
*/ */
router.get('/', async (_req: Request, res: Response) => { router.get('/', async (_req: Request, res: Response): Promise<void> => {
try {
// Initialize accounts from existing tokens on first request // Initialize accounts from existing tokens on first request
initializeAccounts(); initializeAccounts();
@@ -83,6 +84,9 @@ router.get('/', async (_req: Request, res: Response) => {
}); });
res.json({ authStatus }); res.json({ authStatus });
} catch (error) {
res.status(500).json({ error: (error as Error).message });
}
}); });
// ==================== Account Management ==================== // ==================== Account Management ====================
@@ -91,11 +95,16 @@ router.get('/', async (_req: Request, res: Response) => {
* GET /api/cliproxy/accounts - Get all accounts across all providers * GET /api/cliproxy/accounts - Get all accounts across all providers
*/ */
router.get('/accounts', (_req: Request, res: Response) => { router.get('/accounts', (_req: Request, res: Response) => {
try {
// Initialize accounts from existing tokens // Initialize accounts from existing tokens
initializeAccounts(); initializeAccounts();
const accounts = getAllAccountsSummary(); const accounts = getAllAccountsSummary();
res.json({ accounts }); res.json({ accounts });
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to list accounts';
res.status(500).json({ error: message });
}
}); });
/** /**
@@ -110,8 +119,13 @@ router.get('/accounts/:provider', (req: Request, res: Response): void => {
return; return;
} }
try {
const accounts = getProviderAccounts(provider as CLIProxyProvider); const accounts = getProviderAccounts(provider as CLIProxyProvider);
res.json({ provider, accounts }); res.json({ provider, accounts });
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get provider accounts';
res.status(500).json({ error: message });
}
}); });
/** /**
@@ -132,12 +146,19 @@ router.post('/accounts/:provider/default', (req: Request, res: Response): void =
return; return;
} }
try {
const success = setDefaultAccountFn(provider as CLIProxyProvider, accountId); const success = setDefaultAccountFn(provider as CLIProxyProvider, accountId);
if (success) { if (success) {
res.json({ provider, defaultAccount: accountId }); res.json({ provider, defaultAccount: accountId });
} else { } else {
res.status(404).json({ error: 'Account not found' }); res
.status(404)
.json({ error: `Account '${accountId}' not found for provider '${provider}'` });
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to set default account';
res.status(500).json({ error: message });
} }
}); });
@@ -153,12 +174,19 @@ router.delete('/accounts/:provider/:accountId', (req: Request, res: Response): v
return; return;
} }
try {
const success = removeAccountFn(provider as CLIProxyProvider, accountId); const success = removeAccountFn(provider as CLIProxyProvider, accountId);
if (success) { if (success) {
res.json({ provider, accountId, deleted: true }); res.json({ provider, accountId, deleted: true });
} else { } else {
res.status(404).json({ error: 'Account not found' }); res
.status(404)
.json({ error: `Account '${accountId}' not found for provider '${provider}'` });
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to remove account';
res.status(500).json({ error: message });
} }
}); });
+10 -2
View File
@@ -24,12 +24,16 @@ const router = Router();
/** /**
* GET /api/config/format - Return current config format and migration status * GET /api/config/format - Return current config format and migration status
*/ */
router.get('/format', (_req: Request, res: Response) => { router.get('/format', (_req: Request, res: Response): void => {
try {
res.json({ res.json({
format: getConfigFormat(), format: getConfigFormat(),
migrationNeeded: needsMigration(), migrationNeeded: needsMigration(),
backups: getBackupDirectories(), backups: getBackupDirectories(),
}); });
} catch (error) {
res.status(500).json({ error: (error as Error).message });
}
}); });
/** /**
@@ -89,10 +93,14 @@ router.put('/', (req: Request, res: Response): void => {
/** /**
* POST /api/config/migrate - Trigger migration from JSON to YAML * POST /api/config/migrate - Trigger migration from JSON to YAML
*/ */
router.post('/migrate', async (req: Request, res: Response) => { router.post('/migrate', async (req: Request, res: Response): Promise<void> => {
try {
const dryRun = req.query.dryRun === 'true'; const dryRun = req.query.dryRun === 'true';
const result = await migrate(dryRun); const result = await migrate(dryRun);
res.json(result); res.json(result);
} catch (error) {
res.status(500).json({ error: (error as Error).message });
}
}); });
/** /**
+9 -1
View File
@@ -10,15 +10,20 @@ const router = Router();
/** /**
* GET /api/health - Run health checks * GET /api/health - Run health checks
*/ */
router.get('/', async (_req: Request, res: Response) => { router.get('/', async (_req: Request, res: Response): Promise<void> => {
try {
const report = await runHealthChecks(); const report = await runHealthChecks();
res.json(report); res.json(report);
} catch (error) {
res.status(500).json({ error: (error as Error).message });
}
}); });
/** /**
* POST /api/health/fix/:checkId - Fix a health issue * POST /api/health/fix/:checkId - Fix a health issue
*/ */
router.post('/fix/:checkId', (req: Request, res: Response): void => { router.post('/fix/:checkId', (req: Request, res: Response): void => {
try {
const { checkId } = req.params; const { checkId } = req.params;
const result = fixHealthIssue(checkId); const result = fixHealthIssue(checkId);
@@ -27,6 +32,9 @@ router.post('/fix/:checkId', (req: Request, res: Response): void => {
} else { } else {
res.status(400).json({ success: false, message: result.message }); res.status(400).json({ success: false, message: result.message });
} }
} catch (error) {
res.status(500).json({ error: (error as Error).message });
}
}); });
export default router; export default router;