diff --git a/cmd/root/api.go b/cmd/root/api.go index f4ce5501f..804a059b2 100644 --- a/cmd/root/api.go +++ b/cmd/root/api.go @@ -46,7 +46,7 @@ func newAPICmd() *cobra.Command { return cmd } -func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error { +func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) (retErr error) { telemetry.TrackCommand("serve", append([]string{"api"}, args...)) ctx := cmd.Context() @@ -65,6 +65,10 @@ func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error { defer func() { if err := cleanup(); err != nil { slog.Error("Failed to cleanup fake proxy", "error", err) + // Only set return error if no other error occurred + if retErr == nil { + retErr = fmt.Errorf("failed to cleanup fake proxy: %w", err) + } } }() @@ -76,6 +80,10 @@ func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error { defer func() { if err := recordCleanup(); err != nil { slog.Error("Failed to cleanup recording proxy", "error", err) + // Only set return error if no other error occurred + if retErr == nil { + retErr = fmt.Errorf("failed to cleanup recording proxy: %w", err) + } } }() @@ -87,7 +95,14 @@ func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error { if err != nil { return err } - defer lnCleanup() + defer func() { + if err := lnCleanup(); err != nil { + slog.Error("Failed to cleanup listener", "error", err) + if retErr == nil { + retErr = fmt.Errorf("failed to cleanup listener: %w", err) + } + } + }() out.Println("Listening on", ln.Addr().String()) @@ -106,6 +121,9 @@ func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error { defer func() { if err := sessionStore.Close(); err != nil { slog.Error("Failed to close session store", "error", err) + if retErr == nil { + retErr = fmt.Errorf("failed to close session store: %w", err) + } } }() diff --git a/cmd/root/run.go b/cmd/root/run.go index 52eef0751..a5648570d 100644 --- a/cmd/root/run.go +++ b/cmd/root/run.go @@ -141,7 +141,7 @@ func (f *runExecFlags) runRunCommand(cmd *cobra.Command, args []string) error { return f.runOrExec(ctx, out, args, useTUI) } -func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []string, useTUI bool) error { +func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []string, useTUI bool) (retErr error) { slog.Debug("Starting agent", "agent", f.agentName) // Start CPU profiling if requested @@ -216,6 +216,9 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s defer func() { if err := fakeCleanup(); err != nil { slog.Error("Failed to cleanup fake proxy", "error", err) + if retErr == nil { + retErr = fmt.Errorf("failed to cleanup fake proxy: %w", err) + } } }() @@ -228,6 +231,9 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s defer func() { if err := recordCleanup(); err != nil { slog.Error("Failed to cleanup recording proxy", "error", err) + if retErr == nil { + retErr = fmt.Errorf("failed to cleanup recording proxy: %w", err) + } } }() out.Println("Recording mode enabled, cassette: " + cassettePath) @@ -260,6 +266,9 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s defer func() { if err := rt.Close(); err != nil { slog.Error("Failed to close runtime", "error", err) + if retErr == nil { + retErr = fmt.Errorf("failed to close runtime: %w", err) + } } }() var initialTeamCleanupOnce sync.Once