expanderr icon indicating copy to clipboard operation
expanderr copied to clipboard

Feature: ability to automatically wrap errors?

Open arp242 opened this issue 7 years ago • 1 comments

You frequently want to add a function call to errors, such as:

return fmt.Errorf("cannot parse %v: %v", input, err)

or:

return errors.Wrap(err)

It would be nice if you could do something like:

$ expanderr -wrap errors.Wrap err.go:#42
$ expanderr -wrap fmt.Errorf err.go:#42

Which would result in:

return fmt.Errorf(|err)
return errors.Wrap(|err)

The pipe is the location of the cursor; the location of that could be added in the -format json output.

I can take a look at this later if feel this would be a good addition.

arp242 avatar Sep 21 '17 13:09 arp242

The feature itself is welcome, but I’m not entirely sure yet about how it should look like:

I have to admit that I don’t have more experience with error-wrapping packages than the pure knowledge of github.com/pkg/errors’s existence. I know that there are others, and I don’t know whether they are all used the same way, or if there are multiple legit ways to use these packages.

Regarding your suggestion, I think adding errors.Wrap is fine, but just adding fmt.Errorf seems too brittle — I think we should make it fmt.Errorf("%v", err) at least.

I’m not sure how much customization is necessary/warranted here. I.e., would -wrap identify one of multiple pre-defined wrapping strategies? It seems like any kind of templating support would open a can of worms :)

stapelberg avatar Sep 21 '17 19:09 stapelberg