diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-16 03:19:25 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-16 03:19:25 +0200 |
| commit | 5cf8526fd81dadd181f30b1d4c862ba1f1b2a5b1 (patch) | |
| tree | 94f53fe86f0b1bd742a67cad9fe7de095046ee02 | |
| parent | 8790ff6f699d1b073d906b2752f714a0ed3ee2ae (diff) | |
Replace panic with graceful error returns in tmuxedit and mcp-server
Change initDebugLog() and defaultLogPath() to return errors instead of
panicking. Callers now handle errors gracefully with proper messages.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| -rw-r--r-- | cmd/hexai-mcp-server/main.go | 14 | ||||
| -rw-r--r-- | cmd/hexai-mcp-server/main_test.go | 5 | ||||
| -rw-r--r-- | internal/tmuxedit/run.go | 20 |
3 files changed, 24 insertions, 15 deletions
diff --git a/cmd/hexai-mcp-server/main.go b/cmd/hexai-mcp-server/main.go index 1f52616..32817f3 100644 --- a/cmd/hexai-mcp-server/main.go +++ b/cmd/hexai-mcp-server/main.go @@ -56,7 +56,11 @@ type mcpOptions struct { func main() { printDeprecationWarning() - defaultLog := defaultLogPath() + defaultLog, err := defaultLogPath() + if err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } logPath := flag.String("log", defaultLog, "path to log file (optional)") configPath := flag.String("config", "", "path to config file (optional)") promptsDir := flag.String("prompts-dir", "", "path to prompts directory (optional)") @@ -108,11 +112,11 @@ func run(opts mcpOptions, stdin io.Reader, stdout, stderr io.Writer) error { } // defaultLogPath returns the default MCP log file path in the state directory. -// Panics if state directory cannot be created. -func defaultLogPath() string { +// Returns an error if the state directory cannot be created. +func defaultLogPath() (string, error) { stateDir, err := appconfig.StateDir() if err != nil { - panic(fmt.Sprintf("cannot create state directory: %v", err)) + return "", fmt.Errorf("cannot create state directory: %w", err) } - return fmt.Sprintf("%s/hexai-mcp-server.log", stateDir) + return fmt.Sprintf("%s/hexai-mcp-server.log", stateDir), nil } diff --git a/cmd/hexai-mcp-server/main_test.go b/cmd/hexai-mcp-server/main_test.go index cf48954..76d0fca 100644 --- a/cmd/hexai-mcp-server/main_test.go +++ b/cmd/hexai-mcp-server/main_test.go @@ -41,7 +41,10 @@ func TestPrintDeprecationWarning(t *testing.T) { } func TestDefaultLogPath(t *testing.T) { - path := defaultLogPath() + path, err := defaultLogPath() + if err != nil { + t.Fatalf("defaultLogPath returned error: %v", err) + } if path == "" { t.Fatal("expected non-empty log path") } diff --git a/internal/tmuxedit/run.go b/internal/tmuxedit/run.go index a156e5f..efa503b 100644 --- a/internal/tmuxedit/run.go +++ b/internal/tmuxedit/run.go @@ -111,23 +111,23 @@ func loadConfig(configPath string) appconfig.App { // debugLog is the debug logger. Set to a real logger via initDebugLog(). var debugLog *log.Logger -// initDebugLog creates a debug log file at /tmp/hexai-tmux-edit.log. -// initDebugLog creates a debug log file in the state directory (~/.local/state/hexai/hexai-tmux-edit.log). -// Falls back to /tmp if state directory cannot be created. -// initDebugLog creates a debug log file in the state directory (~/.local/state/hexai/hexai-tmux-edit.log). -// Panics if the state directory cannot be created. -func initDebugLog() { +// initDebugLog creates a debug log file in the state directory +// (~/.local/state/hexai/hexai-tmux-edit.log). Returns an error if the +// state directory cannot be resolved. Silently skips logging if the +// log file itself cannot be opened. +func initDebugLog() error { stateDir, err := appconfig.StateDir() if err != nil { - panic(fmt.Sprintf("cannot create state directory: %v", err)) + return fmt.Errorf("cannot create state directory: %w", err) } logPath := filepath.Join(stateDir, "hexai-tmux-edit.log") f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644) if err != nil { - return + return nil } debugLog = log.New(f, "", log.LstdFlags|log.Lmicroseconds) + return nil } func dbg(format string, args ...any) { @@ -140,7 +140,9 @@ func dbg(format string, args ...any) { // It resolves the agent (by name or auto-detect), extracts the current // prompt, opens the editor popup, then clears and sends the result. func runWithConfig(opts Options, cfg appconfig.App) error { - initDebugLog() + if err := initDebugLog(); err != nil { + return fmt.Errorf("init debug log: %w", err) + } dbg("=== hexai-tmux-edit start ===") dbg("opts: pane=%q agent=%q config=%q", opts.Pane, opts.Agent, opts.ConfigPath) |
