Fix panic in helmfile init when parsing invalid helm versions

Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-08-14 04:33:35 +00:00
parent 7fbbe606d6
commit b8b9cad2ff
2 changed files with 57 additions and 2 deletions

View File

@ -1,7 +1,6 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -35,7 +34,8 @@ func toCLIError(g *config.GlobalImpl, err error) error {
case *app.Error:
return errors.NewExitError(e.Error(), e.Code())
default:
panic(fmt.Errorf("BUG: please file an github issue for this unhandled error: %T: %v", e, e))
// Handle all other errors gracefully by returning an exit error with code 1
return errors.NewExitError(e.Error(), 1)
}
}
return err

55
cmd/root_error_test.go Normal file
View File

@ -0,0 +1,55 @@
package cmd
import (
"testing"
"fmt"
"github.com/helmfile/helmfile/pkg/config"
)
// TestToCLIErrorHandlesStandardErrors tests that toCLIError properly handles
// standard Go errors instead of panicking. This test prevents regression of
// the issue fixed in https://github.com/helmfile/helmfile/issues/2119
func TestToCLIErrorHandlesStandardErrors(t *testing.T) {
globalImpl := &config.GlobalImpl{}
tests := []struct {
name string
err error
expectPanic bool
}{
{
name: "nil error should not panic",
err: nil,
expectPanic: false,
},
{
name: "standard fmt.Errorf should not panic",
err: fmt.Errorf("error find helm srmver version '%s': unable to find semver info", "invalid"),
expectPanic: false,
},
{
name: "standard error string should not panic",
err: fmt.Errorf("empty helm version"),
expectPanic: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if !tt.expectPanic {
t.Errorf("toCLIError() panicked unexpectedly: %v", r)
}
} else {
if tt.expectPanic {
t.Errorf("toCLIError() expected to panic but did not")
}
}
}()
_ = toCLIError(globalImpl, tt.err)
})
}
}