refactor: selective catches in gstack-global-discover

Convert 8 defensive catch blocks to selective error handling. Filesystem
ops check ENOENT/EACCES, process ops check exit status. Unexpected errors
now propagate instead of returning silent defaults.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-04-09 04:54:31 -10:00
parent 4166a98831
commit fb24bb4a1c

View File

@@ -167,8 +167,11 @@ function getGitRemote(cwd: string): string | null {
stdio: ["pipe", "pipe", "pipe"], stdio: ["pipe", "pipe", "pipe"],
}).trim(); }).trim();
return remote || null; return remote || null;
} catch { } catch (err: any) {
return null; // Expected: no remote configured, repo not found, git not installed
if (err?.status !== undefined) return null; // non-zero exit from git
if (err?.code === 'ENOENT') return null; // git binary not found
throw err;
} }
} }
@@ -183,8 +186,9 @@ function scanClaudeCode(since: Date): Session[] {
let dirs: string[]; let dirs: string[];
try { try {
dirs = readdirSync(projectsDir); dirs = readdirSync(projectsDir);
} catch { } catch (err: any) {
return []; if (err?.code === 'ENOENT' || err?.code === 'EACCES') return [];
throw err;
} }
for (const dirName of dirs) { for (const dirName of dirs) {
@@ -209,8 +213,9 @@ function scanClaudeCode(since: Date): Session[] {
const hasRecentFile = jsonlFiles.some((f) => { const hasRecentFile = jsonlFiles.some((f) => {
try { try {
return statSync(join(dirPath, f)).mtime >= since; return statSync(join(dirPath, f)).mtime >= since;
} catch { } catch (err: any) {
return false; if (err?.code === 'ENOENT' || err?.code === 'EACCES') return false;
throw err;
} }
}); });
if (!hasRecentFile) continue; if (!hasRecentFile) continue;
@@ -223,8 +228,9 @@ function scanClaudeCode(since: Date): Session[] {
const recentFiles = jsonlFiles.filter((f) => { const recentFiles = jsonlFiles.filter((f) => {
try { try {
return statSync(join(dirPath, f)).mtime >= since; return statSync(join(dirPath, f)).mtime >= since;
} catch { } catch (err: any) {
return false; if (err?.code === 'ENOENT' || err?.code === 'EACCES') return false;
throw err;
} }
}); });
for (let i = 0; i < recentFiles.length; i++) { for (let i = 0; i < recentFiles.length; i++) {
@@ -251,8 +257,9 @@ function resolveClaudeCodeCwd(
.map((f) => { .map((f) => {
try { try {
return { name: f, mtime: statSync(join(dirPath, f)).mtime.getTime() }; return { name: f, mtime: statSync(join(dirPath, f)).mtime.getTime() };
} catch { } catch (err: any) {
return null; if (err?.code === 'ENOENT' || err?.code === 'EACCES') return null;
throw err;
} }
}) })
.filter(Boolean) .filter(Boolean)
@@ -381,8 +388,9 @@ function scanGemini(since: Date): Session[] {
let projectDirs: string[]; let projectDirs: string[];
try { try {
projectDirs = readdirSync(tmpDir); projectDirs = readdirSync(tmpDir);
} catch { } catch (err: any) {
return []; if (err?.code === 'ENOENT' || err?.code === 'EACCES') return [];
throw err;
} }
for (const projectName of projectDirs) { for (const projectName of projectDirs) {