Skip to content

Commit

Permalink
refactor: change ErrInvalidPath to *
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Sep 6, 2023
1 parent bf1947a commit dc9fa1a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions path/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ type ErrInvalidPath struct {
path string
}

func (e ErrInvalidPath) Error() string {
func (e *ErrInvalidPath) Error() string {
return fmt.Sprintf("invalid path %q: %s", e.path, e.err)
}

func (e ErrInvalidPath) Unwrap() error {
func (e *ErrInvalidPath) Unwrap() error {
return e.err
}

func (e ErrInvalidPath) Is(err error) bool {
func (e *ErrInvalidPath) Is(err error) bool {
switch err.(type) {
case ErrInvalidPath, *ErrInvalidPath:
case *ErrInvalidPath:
return true
default:
return false
Expand Down
6 changes: 1 addition & 5 deletions path/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (
)

func TestErrorIs(t *testing.T) {
if !errors.Is(ErrInvalidPath{path: "foo", err: errors.New("bar")}, ErrInvalidPath{}) {
t.Fatal("error must be error")
}

if !errors.Is(&ErrInvalidPath{path: "foo", err: errors.New("bar")}, ErrInvalidPath{}) {
if !errors.Is(&ErrInvalidPath{path: "foo", err: errors.New("bar")}, &ErrInvalidPath{}) {
t.Fatal("pointer to error must be error")
}
}
2 changes: 1 addition & 1 deletion path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type immutablePath struct {

func NewImmutablePath(p Path) (ImmutablePath, error) {
if p.Namespace().Mutable() {
return nil, ErrInvalidPath{err: ErrExpectedImmutable, path: p.String()}
return nil, &ErrInvalidPath{err: ErrExpectedImmutable, path: p.String()}
}

segments := p.Segments()
Expand Down
6 changes: 3 additions & 3 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestNewPath(t *testing.T) {
{"/ipfs/", ErrInsufficientComponents},
{"ipfs/", ErrInsufficientComponents},
{"ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n", ErrInsufficientComponents},
{"/ipld/foo", ErrInvalidPath{}},
{"/ipld/foo", &ErrInvalidPath{}},
{"/ipld/", ErrInsufficientComponents},
{"ipld/", ErrInsufficientComponents},
{"ipld/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n", ErrInsufficientComponents},
Expand All @@ -102,7 +102,7 @@ func TestNewPath(t *testing.T) {
for _, testCase := range testCases {
_, err := NewPath(testCase.src)
assert.ErrorIs(t, err, testCase.err)
assert.ErrorIs(t, err, ErrInvalidPath{}) // Always an ErrInvalidPath!
assert.ErrorIs(t, err, &ErrInvalidPath{}) // Always an ErrInvalidPath!
}
})

Expand Down Expand Up @@ -218,7 +218,7 @@ func TestNewImmutablePath(t *testing.T) {

_, err = NewImmutablePath(p)
assert.ErrorIs(t, err, ErrExpectedImmutable)
assert.ErrorIs(t, err, ErrInvalidPath{})
assert.ErrorIs(t, err, &ErrInvalidPath{})
}
})

Expand Down

0 comments on commit dc9fa1a

Please sign in to comment.