fix: NPE due to checking if error is nil when it can be a value

Fixes #468
This commit is contained in:
Alec Thomas
2024-11-06 09:35:14 +11:00
parent c90c6732cc
commit f388f6cd39
2 changed files with 20 additions and 1 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ func callFunction(f reflect.Value, bindings bindings) error {
return err
}
ferr := out[0]
if ferrv := reflect.ValueOf(ferr); !ferrv.IsValid() || ferrv.IsNil() {
if ferrv := reflect.ValueOf(ferr); !ferrv.IsValid() || ((ferrv.Kind() == reflect.Interface || ferrv.Kind() == reflect.Pointer) && ferrv.IsNil()) {
return nil
}
return ferr.(error) //nolint:forcetypeassert
+19
View File
@@ -127,3 +127,22 @@ func TestFlagNamer(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "SOMEFLAG", app.Model.Flags[1].Name)
}
type npError string
func (e npError) Error() string {
return "ERROR: " + string(e)
}
func TestCallbackNonPointerError(t *testing.T) {
method := func() error {
return npError("failed")
}
var cli struct{}
p, err := New(&cli)
assert.NoError(t, err)
err = callFunction(reflect.ValueOf(method), p.bindings)
assert.EqualError(t, err, "ERROR: failed")
}