Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix(cli): report user-facing command errors (#1552)
sworddut5d345143bf1d
diff --git a/cli/cmd/openlit/main.go b/cli/cmd/openlit/main.goindex 9dd90b6f..c9076086 100644--- a/cli/cmd/openlit/main.go+++ b/cli/cmd/openlit/main.go@@ -11,6 +11,8 @@package mainimport (+ "fmt"+ "io""os""github.com/openlit/openlit/cli/internal/coding"@@ -21,15 +23,21 @@ import ()func main() {+ if execute(os.Args[1:], os.Stdout, os.Stderr) != 0 {+ os.Exit(1)+ }+}++func execute(args []string, stdout, stderr io.Writer) int {root := newRootCmd()+ root.SetArgs(args)+ root.SetOut(stdout)+ root.SetErr(stderr)if err := root.Execute(); err != nil {- // cobra already prints to stderr; just set the exit code.- // We never use os.Exit on telemetry-path errors; those swallow- // inside the command handlers themselves so the agent never- // sees a non-zero exit. Errors here are misuse (bad flags,- // unknown subcommands) where exiting non-zero is correct.- os.Exit(1)+ fmt.Fprintf(stderr, "openlit: %v\n", err)+ return 1}+ return 0}func newRootCmd() *cobra.Command {@@ -55,13 +63,12 @@ Configure the OTLP endpoint and (optional) API key via:- or std: OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS- or file: ~/.config/openlit/config.env (allow-listed keys)`,- // Suppress cobra's default usage spam on errors that bubble up- // from subcommands; the subcommands handle their own messaging.+ // Suppress cobra's default usage spam on errors that bubble up;+ // execute reports the concise error instead.SilenceUsage: true,- // Errors are printed by cobra by default. We keep that for misuse- // errors (bad flags) but swallow telemetry-path errors inside the- // subcommand handlers so they never leak to stdout/stderr at all.- SilenceErrors: false,+ // execute prints returned command errors exactly once. Telemetry-path+ // failures are swallowed inside their handlers and never reach it.+ SilenceErrors: true,}registerSubcommands(root)diff --git a/cli/cmd/openlit/main_test.go b/cli/cmd/openlit/main_test.gonew file mode 100644index 00000000..8ef89b83--- /dev/null+++ b/cli/cmd/openlit/main_test.go@@ -0,0 +1,55 @@+package main++import (+ "bytes"+ "testing"+)++func TestExecuteReportsUserFacingErrors(t *testing.T) {+ tests := []struct {+ name string+ args []string+ wantText string+ }{+ {+ name: "unknown doctor flag",+ args: []string{"doctor", "--definitely-invalid"},+ wantText: "unknown flag: --definitely-invalid",+ },+ {+ name: "invalid configure value",+ args: []string{"configure", "--content-capture", "oops"},+ wantText: `invalid --content-capture "oops" (allowed: minimal, metadata_only, full)`,+ },+ {+ name: "missing install vendor",+ args: []string{"coding", "install"},+ wantText: `required flag(s) "vendor" not set`,+ },+ {+ name: "unknown root command",+ args: []string{"definitely-invalid"},+ wantText: `unknown command "definitely-invalid" for "openlit"`,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ var stdout bytes.Buffer+ var stderr bytes.Buffer++ exitCode := execute(tt.args, &stdout, &stderr)++ if exitCode != 1 {+ t.Fatalf("execute() exit code = %d, want 1", exitCode)+ }+ if stdout.Len() != 0 {+ t.Errorf("execute() stdout = %q, want empty", stdout.String())+ }+ wantStderr := "openlit: " + tt.wantText + "\n"+ if got := stderr.String(); got != wantStderr {+ t.Errorf("execute() stderr = %q, want %q", got, wantStderr)+ }+ })+ }+}