Skip to content

Drop automatic prefix in error messages returned by Run()? #383

Description

@jotaen

It seems that all error messages, which are returned by a Run() method of a command struct, are prefixed with the call chain of the program.

Consider the following sample program:

package main

import (
	"errors"
	"fmt"
	"github.com/alecthomas/kong"
)

type Context struct {}

type Cmd struct {}

func (c *Cmd) Run(ctx *Context) error {
	return errors.New("Command failed.")
}

var cli struct {
	Cmd Cmd `cmd:"" default:"1"`
}

func main() {
	ctx := kong.Parse(&cli)
	err := ctx.Run(&Context{})
	if err != nil {
		fmt.Println(err)
	}
}

In my application, the error messages returned by Run() are end-user facing, so the “call-chain” prefix wouldn’t mean anything to my users and might potentially confuse them.

Would it be possible to drop the prefixing, and just pass on the original error object as is? I.e., without wrapping, modifying, or re-serializing the error object at all?

It seems that the new behaviour was introduced in #344, where callbacks.go now re-serializes the original error message, in order to add the prefix info:

return fmt.Errorf("%s.%s(): %w", v.Type(), name, err)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions