Skip to content

Instantly share code, notes, and snippets.

@koshatul
Last active November 18, 2020 06:55
Show Gist options
  • Save koshatul/5b9195dbac9db10c3606840d07f6ce90 to your computer and use it in GitHub Desktop.
Save koshatul/5b9195dbac9db10c3606840d07f6ce90 to your computer and use it in GitHub Desktop.
Testing errorlint

Error wrapping test

$ golangci-lint --version
golangci-lint has version 1.32.2 built from 52d26a3 on 2020-11-03T01:15:38Z
$ golangci-lint run --enable-all ./...
main.go:33:45: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
        return fmt.Errorf("%w: %s", ErrFooFailed, err.Error())
                                                  ^
package main
import (
"errors"
"fmt"
"log"
)
/*
* Pretend this in in "package foo".
*/
var (
// ErrParsingError is in the foo package.
ErrParsingError = errors.New("parsing error")
)
// Foo is in a different package.
func Foo() error {
return fmt.Errorf("%w at position 10", ErrParsingError)
}
/*
* Pretend this in in "package bar".
*/
var (
// ErrFooFailed is in the bar package.
ErrFooFailed = errors.New("foo failed")
)
// Bar is in this package.
func Bar() error {
if err := Foo(); err != nil {
return fmt.Errorf("%w: %s", ErrFooFailed, err.Error())
}
return nil
}
/*
* Pretend this in in "package main", even though it is.
*/
func main() {
log.Println("Starting")
if err := Bar(); err != nil {
if errors.Is(err, ErrFooFailed) {
log.Print(err)
} else {
log.Printf("Unknown error: %s", err.Error())
}
}
log.Println("Finished")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment