Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit 137520c

Browse files
committed
remove fmt.Errorf
1 parent db809c7 commit 137520c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

package_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"reflect"
99
"runtime"
1010
"testing"
11+
12+
"github.com/pkg/errors"
1113
)
1214

1315
func testContext(t *testing.T, opts ...func(*Context) error) *Context {
@@ -28,13 +30,14 @@ func TestResolvePackage(t *testing.T) {
2830
pkg: "a",
2931
}, {
3032
pkg: "localimport",
31-
err: fmt.Errorf(`import "../localimport": relative import not supported`),
33+
err: &importErr{path: "../localimport", msg: "relative import not supported"},
3234
}}
3335
proj := testProject(t)
3436
for _, tt := range tests {
3537
ctx, err := NewContext(proj, tt.opts...)
3638
defer ctx.Destroy()
3739
_, err = ctx.ResolvePackage(tt.pkg)
40+
err = errors.Cause(err)
3841
if !reflect.DeepEqual(err, tt.err) {
3942
t.Errorf("ResolvePackage(%q): want: %v, got %v", tt.pkg, tt.err, err)
4043
}

resolver.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,26 @@ type importer struct {
7272
Root string // root directory
7373
}
7474

75+
type importErr struct {
76+
path string
77+
msg string
78+
}
79+
80+
func (e *importErr) Error() string {
81+
return fmt.Sprintf("import %q: %v", e.path, e.msg)
82+
}
83+
7584
func (i *importer) Import(path string) (*build.Package, error) {
7685
if path == "" {
77-
return nil, fmt.Errorf("import %q: invalid import path", path)
86+
return nil, errors.WithStack(&importErr{path: path, msg: "invalid import path"})
7887
}
7988

8089
if path == "." || path == ".." || strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../") {
81-
return nil, fmt.Errorf("import %q: relative import not supported", path)
90+
return nil, errors.WithStack(&importErr{path: path, msg: "relative import not supported"})
8291
}
8392

8493
if strings.HasPrefix(path, "/") {
85-
return nil, fmt.Errorf("import %q: cannot import absolute path", path)
94+
return nil, errors.WithStack(&importErr{path: path, msg: "cannot import absolute path"})
8695
}
8796

8897
var p *build.Package

0 commit comments

Comments
 (0)