Named error for no components

Update the previous `invalid path` error to match the error returned
from `SplitAbsPath`.
This commit is contained in:
Travis Person 2015-05-22 09:18:49 -07:00
parent d96246c622
commit 2c71c54823
3 changed files with 16 additions and 8 deletions

View File

@ -3,7 +3,6 @@ package core
import (
"errors"
"strings"
"fmt"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
@ -32,7 +31,7 @@ func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (*merkledag.Node, er
seg := p.Segments()
if len(seg) < 2 || seg[1] == "" { // just "/<protocol/>" without further segments
return nil, fmt.Errorf("invalid path: %s", string(p))
return nil, path.ErrNoComponents
}
extensions := seg[2:]

View File

@ -4,18 +4,22 @@ import (
"testing"
path "github.com/ipfs/go-ipfs/path"
"strings"
)
func TestResolveInvalidPath(t *testing.T) {
func TestResolveNoComponents(t *testing.T) {
n, err := NewMockNode()
if n == nil || err != nil {
t.Fatal("Should have constructed.", err)
t.Fatal("Should have constructed a mock node", err)
}
_, err = Resolve(n.Context(), n, path.Path("/ipns/"))
if err != path.ErrNoComponents {
t.Fatal("Should error with no components (/ipns/).", err)
}
_, err = Resolve(n.Context(), n, path.Path("/ipfs/"))
if !strings.HasPrefix(err.Error(), "invalid path") {
t.Fatal("Should get invalid path.", err)
if err != path.ErrNoComponents {
t.Fatal("Should error with no components (/ipfs/).", err)
}
}

View File

@ -4,6 +4,7 @@ package path
import (
"fmt"
"time"
"errors"
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
@ -14,6 +15,10 @@ import (
var log = u.Logger("path")
// Paths after a protocol must contain at least one component
var ErrNoComponents = errors.New(
"path must contain at least one component")
// ErrNoLink is returned when a link is not found in a path
type ErrNoLink struct {
name string
@ -43,7 +48,7 @@ func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {
// if nothing, bail.
if len(parts) == 0 {
return nil, nil, fmt.Errorf("ipfs path must contain at least one component")
return nil, nil, ErrNoComponents
}
// first element in the path is a b58 hash (for now)