Skip to content

Commit 0128a8e

Browse files
committed
errors: use NoSuchObject error whenever object is missing
Now that we have a consistent type for reporting whether an error is due to an object being missing (as opposed to, say, a serious filesystem error), use that error consistently whenever we have a missing object. Update the tests to expect this new value as well.
1 parent f0b3d14 commit 0128a8e

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

file_storer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"io/ioutil"
88
"os"
99
"path/filepath"
10+
11+
"github.com/git-lfs/gitobj/errors"
1012
)
1113

1214
// fileStorer implements the storer interface by writing to the .git/objects
@@ -34,7 +36,11 @@ func newFileStorer(root, tmp string) *fileStorer {
3436
// It is the caller's responsibility to close the given file "f" after its use
3537
// is complete.
3638
func (fs *fileStorer) Open(sha []byte) (f io.ReadCloser, err error) {
37-
return fs.open(fs.path(sha), os.O_RDONLY)
39+
f, err = fs.open(fs.path(sha), os.O_RDONLY)
40+
if os.IsNotExist(err) {
41+
return nil, errors.NoSuchObject(sha)
42+
}
43+
return f, err
3844
}
3945

4046
// Store implements the storer.Store function and returns the number of bytes

memory_storer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"os"
87
"sync"
8+
9+
"github.com/git-lfs/gitobj/errors"
910
)
1011

1112
// memoryStorer is an implementation of the storer interface that holds data for
@@ -57,7 +58,7 @@ func (ms *memoryStorer) Open(sha []byte) (f io.ReadCloser, err error) {
5758

5859
key := fmt.Sprintf("%x", sha)
5960
if _, ok := ms.fs[key]; !ok {
60-
return nil, os.ErrNotExist
61+
return nil, errors.NoSuchObject(sha)
6162
}
6263
return ms.fs[key], nil
6364
}

memory_storer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"encoding/hex"
66
"io"
77
"io/ioutil"
8-
"os"
98
"strings"
109
"testing"
1110

11+
"github.com/git-lfs/gitobj/errors"
1212
"github.com/stretchr/testify/assert"
1313
)
1414

@@ -47,7 +47,7 @@ func TestMemoryStorerDoesntOpenMissingEntries(t *testing.T) {
4747
ms := newMemoryStorer(nil)
4848

4949
f, err := ms.Open(hex)
50-
assert.Equal(t, os.ErrNotExist, err)
50+
assert.Equal(t, errors.NoSuchObject(hex), err)
5151
assert.Nil(t, f)
5252
}
5353

object_db.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"sync/atomic"
1111

12+
"github.com/git-lfs/gitobj/errors"
1213
"github.com/git-lfs/gitobj/pack"
1314
)
1415

@@ -266,7 +267,7 @@ func (o *ObjectDatabase) save(sha []byte, buf io.Reader) ([]byte, int64, error)
266267
func (o *ObjectDatabase) open(sha []byte) (*ObjectReader, error) {
267268
f, err := o.s.Open(sha)
268269
if err != nil {
269-
if !os.IsNotExist(err) {
270+
if !errors.IsNoSuchObject(err) {
270271
// If there was some other issue beyond not being able
271272
// to find the object, return that immediately and don't
272273
// try and fallback to the *git.ObjectScanner.

pack/set.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"path/filepath"
77
"regexp"
88
"sort"
9+
10+
"github.com/git-lfs/gitobj/errors"
911
)
1012

1113
// Set allows access of objects stored across a set of packfiles.
@@ -162,8 +164,8 @@ type iterFn func(p *Packfile) (o *Object, err error)
162164
// returned immediately, if the error is not ErrIsNotFound, or b) continued
163165
// immediately, if the error is ErrNotFound.
164166
//
165-
// If no packfiles match the given file, return ErrIsNotFound, along with no
166-
// object.
167+
// If no packfiles match the given file, return errors.NoSuchObject, along with
168+
// no object.
167169
func (s *Set) each(name []byte, fn iterFn) (*Object, error) {
168170
var key byte
169171
if len(name) > 0 {
@@ -181,5 +183,5 @@ func (s *Set) each(name []byte, fn iterFn) (*Object, error) {
181183
return o, nil
182184
}
183185

184-
return nil, errNotFound
186+
return nil, errors.NoSuchObject(name)
185187
}

0 commit comments

Comments
 (0)