Skip to content

Commit d409df3

Browse files
committed
feat: add ignorePackageGlobs configuration
1 parent ae2ca99 commit d409df3

File tree

25 files changed

+2396
-19
lines changed

25 files changed

+2396
-19
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ ignoreSigs:
3737
- .WithMessage(
3838
- .WithMessagef(
3939
- .WithStack(
40+
41+
# An array of glob patterns which, if any match the package of the function
42+
# returning the error, will skip wrapcheck analysis for this error. This is
43+
# useful for broadly ignoring packages and/or subpackages from wrapcheck
44+
# analysis. There are no defaults for this value.
45+
ignorePackageGlobs:
46+
- encoding/*
47+
- github.com/pkg/*
4048
```
4149
4250
## Usage

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/tomarrell/wrapcheck/v2
33
go 1.16
44

55
require (
6+
github.com/gobwas/glob v0.2.3
67
github.com/spf13/viper v1.7.1
78
github.com/stretchr/testify v1.6.1
89
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
4545
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
4646
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
4747
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
48+
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
49+
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
4850
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
4951
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
5052
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignorePackageGlobs:
2+
- "encoding/*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ignoreSigs:
2+
ignorePackageGlobs:
3+
- github.com/pkg/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
func main() {
11+
do()
12+
}
13+
14+
func do() error {
15+
err := fmt.Errorf("failed to do something")
16+
if err != nil {
17+
return errors.Wrap(err, "uh oh")
18+
}
19+
20+
if err != nil {
21+
return errors.Wrapf(err, "uh oh")
22+
}
23+
24+
if err != nil {
25+
return errors.WithMessage(err, "uh oh")
26+
}
27+
28+
if err != nil {
29+
return errors.WithMessagef(err, "uh %s", "oh")
30+
}
31+
32+
if err != nil {
33+
return errors.WithStack(err)
34+
}
35+
36+
_, err = json.Marshal(struct{}{})
37+
if err != nil {
38+
return err // want `error returned from external package is unwrapped`
39+
}
40+
41+
return errors.New("uh oh")
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2015, Dave Cheney <[email protected]>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
PKGS := github.com/pkg/errors
2+
SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS))
3+
GO := go
4+
5+
check: test vet gofmt misspell unconvert staticcheck ineffassign unparam
6+
7+
test:
8+
$(GO) test $(PKGS)
9+
10+
vet: | test
11+
$(GO) vet $(PKGS)
12+
13+
staticcheck:
14+
$(GO) get honnef.co/go/tools/cmd/staticcheck
15+
staticcheck -checks all $(PKGS)
16+
17+
misspell:
18+
$(GO) get github.com/client9/misspell/cmd/misspell
19+
misspell \
20+
-locale GB \
21+
-error \
22+
*.md *.go
23+
24+
unconvert:
25+
$(GO) get github.com/mdempsky/unconvert
26+
unconvert -v $(PKGS)
27+
28+
ineffassign:
29+
$(GO) get github.com/gordonklaus/ineffassign
30+
find $(SRCDIRS) -name '*.go' | xargs ineffassign
31+
32+
pedantic: check errcheck
33+
34+
unparam:
35+
$(GO) get mvdan.cc/unparam
36+
unparam ./...
37+
38+
errcheck:
39+
$(GO) get github.com/kisielk/errcheck
40+
errcheck $(PKGS)
41+
42+
gofmt:
43+
@echo Checking code is gofmted
44+
@test -z "$(shell gofmt -s -l -d -e $(SRCDIRS) | tee /dev/stderr)"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge)
2+
3+
Package errors provides simple error handling primitives.
4+
5+
`go get github.com/pkg/errors`
6+
7+
The traditional error handling idiom in Go is roughly akin to
8+
```go
9+
if err != nil {
10+
return err
11+
}
12+
```
13+
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
14+
15+
## Adding context to an error
16+
17+
The errors.Wrap function returns a new error that adds context to the original error. For example
18+
```go
19+
_, err := ioutil.ReadAll(r)
20+
if err != nil {
21+
return errors.Wrap(err, "read failed")
22+
}
23+
```
24+
## Retrieving the cause of an error
25+
26+
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
27+
```go
28+
type causer interface {
29+
Cause() error
30+
}
31+
```
32+
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
33+
```go
34+
switch err := errors.Cause(err).(type) {
35+
case *MyError:
36+
// handle specifically
37+
default:
38+
// unknown error
39+
}
40+
```
41+
42+
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
43+
44+
## Roadmap
45+
46+
With the upcoming [Go2 error proposals](https://go.googlesource.com/proposal/+/master/design/go2draft.md) this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows:
47+
48+
- 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible)
49+
- 1.0. Final release.
50+
51+
## Contributing
52+
53+
Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports.
54+
55+
Before sending a PR, please discuss your change by raising an issue.
56+
57+
## License
58+
59+
BSD-2-Clause
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: build-{build}.{branch}
2+
3+
clone_folder: C:\gopath\src\github.com\pkg\errors
4+
shallow_clone: true # for startup speed
5+
6+
environment:
7+
GOPATH: C:\gopath
8+
9+
platform:
10+
- x64
11+
12+
# http://www.appveyor.com/docs/installed-software
13+
install:
14+
# some helpful output for debugging builds
15+
- go version
16+
- go env
17+
# pre-installed MinGW at C:\MinGW is 32bit only
18+
# but MSYS2 at C:\msys64 has mingw64
19+
- set PATH=C:\msys64\mingw64\bin;%PATH%
20+
- gcc --version
21+
- g++ --version
22+
23+
build_script:
24+
- go install -v ./...
25+
26+
test_script:
27+
- set PATH=C:\gopath\bin;%PATH%
28+
- go test -v ./...
29+
30+
#artifacts:
31+
# - path: '%GOPATH%\bin\*.exe'
32+
deploy: off
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// +build go1.7
2+
3+
package errors
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
stderrors "errors"
10+
)
11+
12+
func noErrors(at, depth int) error {
13+
if at >= depth {
14+
return stderrors.New("no error")
15+
}
16+
return noErrors(at+1, depth)
17+
}
18+
19+
func yesErrors(at, depth int) error {
20+
if at >= depth {
21+
return New("ye error")
22+
}
23+
return yesErrors(at+1, depth)
24+
}
25+
26+
// GlobalE is an exported global to store the result of benchmark results,
27+
// preventing the compiler from optimising the benchmark functions away.
28+
var GlobalE interface{}
29+
30+
func BenchmarkErrors(b *testing.B) {
31+
type run struct {
32+
stack int
33+
std bool
34+
}
35+
runs := []run{
36+
{10, false},
37+
{10, true},
38+
{100, false},
39+
{100, true},
40+
{1000, false},
41+
{1000, true},
42+
}
43+
for _, r := range runs {
44+
part := "pkg/errors"
45+
if r.std {
46+
part = "errors"
47+
}
48+
name := fmt.Sprintf("%s-stack-%d", part, r.stack)
49+
b.Run(name, func(b *testing.B) {
50+
var err error
51+
f := yesErrors
52+
if r.std {
53+
f = noErrors
54+
}
55+
b.ReportAllocs()
56+
for i := 0; i < b.N; i++ {
57+
err = f(0, r.stack)
58+
}
59+
b.StopTimer()
60+
GlobalE = err
61+
})
62+
}
63+
}
64+
65+
func BenchmarkStackFormatting(b *testing.B) {
66+
type run struct {
67+
stack int
68+
format string
69+
}
70+
runs := []run{
71+
{10, "%s"},
72+
{10, "%v"},
73+
{10, "%+v"},
74+
{30, "%s"},
75+
{30, "%v"},
76+
{30, "%+v"},
77+
{60, "%s"},
78+
{60, "%v"},
79+
{60, "%+v"},
80+
}
81+
82+
var stackStr string
83+
for _, r := range runs {
84+
name := fmt.Sprintf("%s-stack-%d", r.format, r.stack)
85+
b.Run(name, func(b *testing.B) {
86+
err := yesErrors(0, r.stack)
87+
b.ReportAllocs()
88+
b.ResetTimer()
89+
for i := 0; i < b.N; i++ {
90+
stackStr = fmt.Sprintf(r.format, err)
91+
}
92+
b.StopTimer()
93+
})
94+
}
95+
96+
for _, r := range runs {
97+
name := fmt.Sprintf("%s-stacktrace-%d", r.format, r.stack)
98+
b.Run(name, func(b *testing.B) {
99+
err := yesErrors(0, r.stack)
100+
st := err.(*fundamental).stack.StackTrace()
101+
b.ReportAllocs()
102+
b.ResetTimer()
103+
for i := 0; i < b.N; i++ {
104+
stackStr = fmt.Sprintf(r.format, st)
105+
}
106+
b.StopTimer()
107+
})
108+
}
109+
GlobalE = stackStr
110+
}

0 commit comments

Comments
 (0)