Skip to content

Commit

Permalink
Drop automatic message prefix from command errors (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaen committed Oct 7, 2023
1 parent 46c0384 commit 93d31e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
8 changes: 0 additions & 8 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,3 @@ func callAnyFunction(f reflect.Value, bindings bindings) (out []any, err error)
}
return out, nil
}

func callMethod(name string, v, f reflect.Value, bindings bindings) error {
err := callFunction(f, bindings)
if err != nil {
return fmt.Errorf("%s.%s(): %w", v.Type(), name, err)
}
return nil
}
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
}

for _, method := range methods {
if err = callMethod("Run", method.node.Target, method.method, method.binds); err != nil {
if err = callFunction(method.method, method.binds); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (k *Kong) applyHook(ctx *Context, name string) error {
binds.add(ctx, trace)
binds.add(trace.Node().Vars().CloneWith(k.vars))
binds.merge(ctx.bindings)
if err := callMethod(name, value, method, binds); err != nil {
if err := callFunction(method, binds); err != nil {
return err
}
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
continue
}
path := &Path{Flag: flag}
if err := callMethod(name, flag.Target, method, binds.clone().add(path)); err != nil {
if err := callFunction(method, binds.clone().add(path)); err != nil {
return next(err)
}
}
Expand Down
17 changes: 17 additions & 0 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,23 @@ func TestRun(t *testing.T) {
assert.Equal(t, "argping", cli.Three.SubCommand.Arg)
}

type failCmd struct{}

func (f failCmd) Run() error {
return errors.New("this command failed")
}

func TestPassesThroughOriginalCommandError(t *testing.T) {
var cli struct {
Fail failCmd `kong:"cmd"`
}
p := mustNew(t, &cli)
ctx, _ := p.Parse([]string{"fail"})
err := ctx.Run()
assert.Error(t, err)
assert.Equal(t, err.Error(), "this command failed")
}

func TestInterpolationIntoModel(t *testing.T) {
var cli struct {
Flag string `default:"${default_value}" help:"Help, I need ${somebody}" enum:"${enum}"`
Expand Down
8 changes: 4 additions & 4 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestBindTo(t *testing.T) {

p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
assert.NoError(t, err)
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
err = callFunction(reflect.ValueOf(method), p.bindings)
assert.NoError(t, err)
assert.Equal(t, "foo", saw)
}
Expand All @@ -58,8 +58,8 @@ func TestInvalidCallback(t *testing.T) {

p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
assert.NoError(t, err)
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
assert.EqualError(t, err, `kong.impl.method(): return value of func(kong.iface) string must implement "error"`)
err = callFunction(reflect.ValueOf(method), p.bindings)
assert.EqualError(t, err, `return value of func(kong.iface) string must implement "error"`)
}

type zrror struct{}
Expand All @@ -83,7 +83,7 @@ func TestCallbackCustomError(t *testing.T) {

p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
assert.NoError(t, err)
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
err = callFunction(reflect.ValueOf(method), p.bindings)
assert.NoError(t, err)
assert.Equal(t, "foo", saw)
}
Expand Down

0 comments on commit 93d31e1

Please sign in to comment.