Named error for `no components`
Update the previous `invalid path` error to match the error returned from `SplitAbsPath`.
Travis Person committed
May 22, 2015 at 09:18 UTC
2c71c54823e4dbd85482625863afc0538d2abc49
3 files changed
+16
-8
core/pathresolver.go
+1
-2
@@ -3,7 +3,6 @@ package core
3
import (
4
"errors"
5
"strings"
6
- "fmt"
6
7
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8
@@ -32,7 +31,7 @@ func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (*merkledag.Node, er
31
seg := p.Segments()
32
33
if len(seg) < 2 || seg[1] == "" { // just "/<protocol/>" without further segments
35
- return nil, fmt.Errorf("invalid path: %s", string(p))
34
+ return nil, path.ErrNoComponents
35
}
36
37
extensions := seg[2:]
core/pathresolver_test.go
+9
-5
@@ -4,18 +4,22 @@ import (
4
"testing"
5
6
path "github.com/ipfs/go-ipfs/path"
7
- "strings"
7
)
8
10
-func TestResolveInvalidPath(t *testing.T) {
9
+func TestResolveNoComponents(t *testing.T) {
10
n, err := NewMockNode()
11
if n == nil || err != nil {
13
- t.Fatal("Should have constructed.", err)
12
+ t.Fatal("Should have constructed a mock node", err)
13
+ }
14
+
15
+ _, err = Resolve(n.Context(), n, path.Path("/ipns/"))
16
+ if err != path.ErrNoComponents {
17
+ t.Fatal("Should error with no components (/ipns/).", err)
18
}
19
20
_, err = Resolve(n.Context(), n, path.Path("/ipfs/"))
17
- if !strings.HasPrefix(err.Error(), "invalid path") {
18
- t.Fatal("Should get invalid path.", err)
21
+ if err != path.ErrNoComponents {
22
+ t.Fatal("Should error with no components (/ipfs/).", err)
23
}
24
25
}
path/resolver.go
+6
-1
@@ -4,6 +4,7 @@ package path
4
import (
5
"fmt"
6
"time"
7
+ "errors"
8
9
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
@@ -14,6 +15,10 @@ import (
15
16
var log = u.Logger("path")
17
18
+// Paths after a protocol must contain at least one component
19
+var ErrNoComponents = errors.New(
20
+ "path must contain at least one component")
21
+
22
// ErrNoLink is returned when a link is not found in a path
23
type ErrNoLink struct {
24
name string
@@ -43,7 +48,7 @@ func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {
48
49
// if nothing, bail.
50
if len(parts) == 0 {
46
- return nil, nil, fmt.Errorf("ipfs path must contain at least one component")
51
+ return nil, nil, ErrNoComponents
52
}
53
54
// first element in the path is a b58 hash (for now)