fix: log non-ENOENT errors in ensureStateDir() instead of silently swallowing

Replace bare catch {} with ENOENT-only silence. Non-ENOENT errors (EACCES,
ENOSPC) are now logged to .gstack/browse-server.log. Includes test for
permission-denied scenario with chmod 444.
This commit is contained in:
Garry Tan
2026-03-14 22:04:25 -05:00
parent 2aa745cb0e
commit 8cb905ec27
2 changed files with 32 additions and 2 deletions
+11 -2
View File
@@ -98,8 +98,17 @@ export function ensureStateDir(config: BrowseConfig): void {
const separator = content.endsWith('\n') ? '' : '\n';
fs.appendFileSync(gitignorePath, `${separator}.gstack/\n`);
}
} catch {
// No .gitignore or unreadable — skip
} catch (err: any) {
if (err.code !== 'ENOENT') {
// Write warning to server log (visible even in daemon mode)
const logPath = path.join(config.stateDir, 'browse-server.log');
try {
fs.appendFileSync(logPath, `[${new Date().toISOString()}] Warning: could not update .gitignore at ${gitignorePath}: ${err.message}\n`);
} catch {
// stateDir write failed too — nothing more we can do
}
}
// ENOENT (no .gitignore) — skip silently
}
}