From 5cf8526fd81dadd181f30b1d4c862ba1f1b2a5b1 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 16 Mar 2026 03:19:25 +0200 Subject: 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 --- cmd/hexai-mcp-server/main.go | 14 +++++++++----- cmd/hexai-mcp-server/main_test.go | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) (limited to 'cmd/hexai-mcp-server') 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") } -- cgit v1.2.3