Skip to content

Commit 6d11e1d

Browse files
committed
fix: safely handle logging and filesystem errors
1 parent 77467ac commit 6d11e1d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ coverage
6767

6868
# Changeset files (optional, some projects want to track these)
6969
# .changeset/
70+
.serena
71+
AGENTS.md
72+

src/modules/logging.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,24 @@ import { writeFileSync, appendFileSync, existsSync } from "fs";
22
import { homedir } from "os";
33
import { join } from "path";
44

5-
const LOG_FILE = join(homedir(), "mcpcat.log");
5+
// Safely determine log file path, handling environments where homedir() may return null
6+
let LOG_FILE: string | null = null;
7+
try {
8+
const home = homedir();
9+
if (home && home !== null && home !== undefined) {
10+
LOG_FILE = join(home, "mcpcat.log");
11+
}
12+
} catch {
13+
// If homedir() or join() fails, LOG_FILE remains null
14+
LOG_FILE = null;
15+
}
616

717
export function writeToLog(message: string): void {
18+
// Skip logging if we don't have a valid log file path
19+
if (!LOG_FILE) {
20+
return;
21+
}
22+
823
const timestamp = new Date().toISOString();
924
const logEntry = `[${timestamp}] ${message}\n`;
1025

0 commit comments

Comments
 (0)