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

Commit

Permalink
remove fmt.Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Dec 13, 2016
1 parent db809c7 commit 137520c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"reflect"
"runtime"
"testing"

"github.com/pkg/errors"
)

func testContext(t *testing.T, opts ...func(*Context) error) *Context {
Expand All @@ -28,13 +30,14 @@ func TestResolvePackage(t *testing.T) {
pkg: "a",
}, {
pkg: "localimport",
err: fmt.Errorf(`import "../localimport": relative import not supported`),
err: &importErr{path: "../localimport", msg: "relative import not supported"},
}}
proj := testProject(t)
for _, tt := range tests {
ctx, err := NewContext(proj, tt.opts...)
defer ctx.Destroy()
_, err = ctx.ResolvePackage(tt.pkg)
err = errors.Cause(err)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("ResolvePackage(%q): want: %v, got %v", tt.pkg, tt.err, err)
}
Expand Down
15 changes: 12 additions & 3 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,26 @@ type importer struct {
Root string // root directory
}

type importErr struct {
path string
msg string
}

func (e *importErr) Error() string {
return fmt.Sprintf("import %q: %v", e.path, e.msg)
}

func (i *importer) Import(path string) (*build.Package, error) {
if path == "" {
return nil, fmt.Errorf("import %q: invalid import path", path)
return nil, errors.WithStack(&importErr{path: path, msg: "invalid import path"})
}

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

if strings.HasPrefix(path, "/") {
return nil, fmt.Errorf("import %q: cannot import absolute path", path)
return nil, errors.WithStack(&importErr{path: path, msg: "cannot import absolute path"})
}

var p *build.Package
Expand Down

0 comments on commit 137520c

Please sign in to comment.