polyfloyd / go-errorlint

A source code linter that can be used to find code that will cause problems with Go's error wrapping scheme
MIT License
248 stars 19 forks source link

Why is `%w` wrong to use? #9

Closed frederikhors closed 3 years ago

frederikhors commented 3 years ago

Dear @polyfloyd,

the great @tomarrell here is suggesting to avoid %w.

Can I ask for your input on the issue so as to better understand what is right to do?

I'm learning Golang day by day.

Thanks!

polyfloyd commented 3 years ago

tomarrell is correct, using %w causes any error wrapped to implicitly become part of the API which is not necessarily desirable.

go-errorlint can check for %w but it is disabled by default for this very reason. See also the caveat in the readme:

When using the -errorf lint, keep in mind that any errors wrapped by fmt.Errorf implicitly become part of your API as according to Hyrum's Law.