Skip to content

Commit 0b06b10

Browse files
committed
Add errs pkg
1 parent a356440 commit 0b06b10

File tree

9 files changed

+53
-39
lines changed

9 files changed

+53
-39
lines changed

baton/baton.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"time"
4040

4141
"github.com/rs/zerolog"
42+
"github.com/wtsi-hgi/ibackup/errs"
4243
"github.com/wtsi-hgi/ibackup/internal"
4344
ex "github.com/wtsi-npg/extendo/v2"
4445
logs "github.com/wtsi-npg/logshim"
@@ -222,7 +223,7 @@ func (b *Baton) ensureCollection(clientIndex int, ri ex.RodsItem) error {
222223
return nil
223224
}
224225

225-
if errors.Is(err, internal.PathError{Msg: ErrOperationTimeout, Path: ri.IPath}) {
226+
if errors.Is(err, errs.PathError{Msg: ErrOperationTimeout, Path: ri.IPath}) {
226227
return err
227228
}
228229

@@ -246,7 +247,7 @@ func timeoutOp(op retry.Operation, path string) error {
246247
case err = <-errCh:
247248
timer.Stop()
248249
case <-timer.C:
249-
err = internal.PathError{Msg: ErrOperationTimeout, Path: path}
250+
err = errs.PathError{Msg: ErrOperationTimeout, Path: path}
250251
}
251252

252253
return err
@@ -619,7 +620,7 @@ func (b *Baton) RemoveFile(path string) error {
619620
}, "remove file error: "+path)
620621

621622
if err != nil && strings.Contains(err.Error(), "CAT_NO_ROWS_FOUND") {
622-
return internal.PathError{Msg: internal.ErrFileDoesNotExist, Path: path}
623+
return errs.PathError{Msg: internal.ErrFileDoesNotExist, Path: path}
623624
}
624625

625626
return err
@@ -645,7 +646,7 @@ func (b *Baton) RemoveDir(path string) error {
645646
}, "remove meta error: "+path)
646647

647648
if err != nil && strings.Contains(err.Error(), "CAT_COLLECTION_NOT_EMPTY") {
648-
return internal.NewDirNotEmpty(path)
649+
return errs.NewDirNotEmptyError(path)
649650
}
650651

651652
return err

internal/error.go renamed to errs/error.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2525
******************************************************************************/
2626

27-
package internal
27+
package errs
2828

2929
import (
3030
"errors"
@@ -53,10 +53,28 @@ func (e PathError) Is(err error) bool {
5353
return false
5454
}
5555

56+
type DirRemovalError struct {
57+
PathError
58+
}
59+
60+
func NewDirError(msg, path string) DirRemovalError {
61+
return DirRemovalError{
62+
PathError{
63+
Msg: "dir removal error: " + msg,
64+
Path: path,
65+
},
66+
}
67+
}
68+
5669
type DirNotEmptyError struct {
5770
PathError
5871
}
5972

60-
func NewDirNotEmpty(path string) DirNotEmptyError {
61-
return DirNotEmptyError{PathError{Msg: "directory not empty", Path: path}}
73+
func NewDirNotEmptyError(path string) DirNotEmptyError {
74+
return DirNotEmptyError{
75+
PathError{
76+
Msg: "directory not empty",
77+
Path: path,
78+
},
79+
}
6280
}

internal/mock.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"strings"
3434
"sync"
3535
"time"
36+
37+
"github.com/wtsi-hgi/ibackup/errs"
3638
)
3739

3840
const ErrMockStatFail = "stat fail"
@@ -101,7 +103,7 @@ func (l *LocalHandler) MakeStatFail(remote string) {
101103
// Returns an error if statFail == Remote.
102104
func (l *LocalHandler) Stat(remote string) (bool, map[string]string, error) {
103105
if l.statFail == remote {
104-
return false, nil, PathError{ErrMockStatFail, ""}
106+
return false, nil, errs.PathError{Msg: ErrMockStatFail, Path: ""}
105107
}
106108

107109
_, err := os.Stat(remote)
@@ -148,7 +150,7 @@ func (l *LocalHandler) MakeRemoveSlow() {
148150
// Put just copies from Local to Remote. Returns an error if putFail == Remote.
149151
func (l *LocalHandler) Put(local, remote string) error {
150152
if l.putFail == remote {
151-
return PathError{ErrMockPutFail, ""}
153+
return errs.PathError{Msg: ErrMockPutFail, Path: ""}
152154
}
153155

154156
if l.putSlow == remote {
@@ -195,7 +197,7 @@ func (l *LocalHandler) MakeMetaFail(remote string) {
195197
// error if metaFail == path.
196198
func (l *LocalHandler) RemoveMeta(path string, meta map[string]string) error {
197199
if l.metaFail == path {
198-
return PathError{ErrMockMetaFail, ""}
200+
return errs.PathError{Msg: ErrMockMetaFail, Path: ""}
199201
}
200202

201203
l.mu.Lock()
@@ -217,7 +219,7 @@ func (l *LocalHandler) RemoveMeta(path string, meta map[string]string) error {
217219
// if metafail == path, or if keys were already defined in the map.
218220
func (l *LocalHandler) AddMeta(path string, meta map[string]string) error {
219221
if l.metaFail == path {
220-
return PathError{ErrMockMetaFail, ""}
222+
return errs.PathError{Msg: ErrMockMetaFail, Path: ""}
221223
}
222224

223225
l.mu.Lock()
@@ -231,7 +233,7 @@ func (l *LocalHandler) AddMeta(path string, meta map[string]string) error {
231233

232234
for key, val := range meta {
233235
if _, exists := pathMeta[key]; exists {
234-
return PathError{ErrMockMetaFail, key}
236+
return errs.PathError{Msg: ErrMockMetaFail, Path: key}
235237
}
236238

237239
pathMeta[key] = val
@@ -304,7 +306,7 @@ func (l *LocalHandler) RemoveDir(path string) error {
304306

305307
err := os.Remove(path)
306308
if err != nil && strings.Contains(err.Error(), "directory not empty") {
307-
return NewDirNotEmpty(path)
309+
return errs.NewDirNotEmptyError(path)
308310
}
309311

310312
return err
@@ -320,7 +322,7 @@ func (l *LocalHandler) RemoveFile(path string) error {
320322

321323
err := os.Remove(path)
322324
if err != nil && strings.Contains(err.Error(), "no such file or directory") {
323-
return PathError{Msg: ErrFileDoesNotExist, Path: path}
325+
return errs.PathError{Msg: ErrFileDoesNotExist, Path: path}
324326
}
325327

326328
return err

put/info.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"syscall"
3333
"time"
3434

35-
"github.com/wtsi-hgi/ibackup/internal"
35+
"github.com/wtsi-hgi/ibackup/errs"
3636
)
3737

3838
const ErrStatFailed = "stat of local path returned strange results"
@@ -77,7 +77,7 @@ func Stat(localPath string) (*ObjectInfo, error) {
7777
func getUserAndGroupFromFileInfo(fi os.FileInfo, localPath string) (string, string, error) {
7878
stat, ok := fi.Sys().(*syscall.Stat_t)
7979
if !ok {
80-
return "", "", internal.PathError{Msg: ErrStatFailed, Path: localPath}
80+
return "", "", errs.PathError{Msg: ErrStatFailed, Path: localPath}
8181
}
8282

8383
u, err := user.LookupId(strconv.Itoa(int(stat.Uid)))

put/put.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939

4040
"github.com/gammazero/workerpool"
4141
"github.com/hashicorp/go-multierror"
42-
"github.com/wtsi-hgi/ibackup/internal"
42+
"github.com/wtsi-hgi/ibackup/errs"
4343
)
4444

4545
const (
@@ -108,7 +108,7 @@ type FileReadTester func(ctx context.Context, path string) error
108108
func headRead(ctx context.Context, path string) error {
109109
out, err := exec.CommandContext(ctx, "head", "-c", "1", path).CombinedOutput()
110110
if err != nil && len(out) > 0 {
111-
err = internal.PathError{Msg: string(out)}
111+
err = errs.PathError{Msg: string(out)}
112112
}
113113

114114
return err
@@ -586,7 +586,7 @@ func (p *Putter) testRead(request *Request) error {
586586
go func() {
587587
select {
588588
case <-timer.C:
589-
errCh <- internal.PathError{Msg: ErrReadTimeout, Path: request.Local}
589+
errCh <- errs.PathError{Msg: ErrReadTimeout, Path: request.Local}
590590
case err := <-readCh:
591591
timer.Stop()
592592
errCh <- err

put/request.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"time"
3434

3535
"github.com/dgryski/go-farm"
36-
"github.com/wtsi-hgi/ibackup/internal"
36+
"github.com/wtsi-hgi/ibackup/errs"
3737
)
3838

3939
type RequestStatus string
@@ -164,13 +164,13 @@ func (r *Request) Prepare() error {
164164
func (r *Request) ValidatePaths() error {
165165
local, err := filepath.Abs(r.Local)
166166
if err != nil {
167-
return internal.PathError{Msg: ErrLocalNotAbs, Path: r.Local}
167+
return errs.PathError{Msg: ErrLocalNotAbs, Path: r.Local}
168168
}
169169

170170
r.Local = local
171171

172172
if !filepath.IsAbs(r.Remote) {
173-
return internal.PathError{Msg: ErrRemoteNotAbs, Path: r.Remote}
173+
return errs.PathError{Msg: ErrRemoteNotAbs, Path: r.Remote}
174174
}
175175

176176
return nil
@@ -407,7 +407,7 @@ func HumgenTransformer(local string) (string, error) {
407407
}
408408

409409
if !dirIsLustreWithPTUSubDir(parts[1], ptuPart, len(parts)) {
410-
return "", internal.PathError{Msg: ErrNotHumgenLustre, Path: local}
410+
return "", errs.PathError{Msg: ErrNotHumgenLustre, Path: local}
411411
}
412412

413413
return fmt.Sprintf("/humgen/%s/%s/%s/%s", parts[ptuPart], parts[ptuPart+1], parts[2],

remove/remove.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,10 @@ import (
3434
"path/filepath"
3535
"strings"
3636

37-
"github.com/wtsi-hgi/ibackup/internal"
37+
"github.com/wtsi-hgi/ibackup/errs"
3838
"github.com/wtsi-hgi/ibackup/put"
3939
)
4040

41-
type DirRemovalError struct {
42-
internal.PathError
43-
}
44-
45-
func newDirError(msg, path string) DirRemovalError {
46-
return DirRemovalError{internal.PathError{Msg: "dir removal error: " + msg, Path: path}}
47-
}
48-
4941
type Handler interface {
5042
// RemoveMeta deletes the given metadata from the given object.
5143
RemoveMeta(path string, meta map[string]string) error
@@ -115,12 +107,12 @@ func RemoveFileAndParentFoldersIfEmpty(handler Handler, path string) error { //n
115107
func removeEmptyFoldersRecursively(handler Handler, path string) error {
116108
err := handler.RemoveDir(path)
117109
if err != nil {
118-
var dirNotEmpty internal.DirNotEmptyError
110+
var dirNotEmpty errs.DirNotEmptyError
119111
if errors.As(err, &dirNotEmpty) {
120112
return nil
121113
}
122114

123-
return newDirError(err.Error(), path)
115+
return errs.NewDirError(err.Error(), path)
124116
}
125117

126118
return removeEmptyFoldersRecursively(handler, filepath.Dir(path))

server/setdb.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/gin-gonic/gin"
4040
gas "github.com/wtsi-hgi/go-authserver"
4141
"github.com/wtsi-hgi/grand"
42+
"github.com/wtsi-hgi/ibackup/errs"
4243
"github.com/wtsi-hgi/ibackup/internal"
4344
"github.com/wtsi-hgi/ibackup/put"
4445
"github.com/wtsi-hgi/ibackup/remove"
@@ -614,7 +615,7 @@ func (s *Server) removeRemoteFileAndHandleHardlink(lpath, rpath string, meta map
614615
transformer put.PathTransformer, entry *set.Entry) error {
615616
err := remove.RemoveFileAndParentFoldersIfEmpty(s.storageHandler, rpath)
616617
if err != nil {
617-
var dirRemovalError remove.DirRemovalError
618+
var dirRemovalError errs.DirRemovalError
618619
if !errors.As(err, &dirRemovalError) {
619620
return err
620621
}

set/inode.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636

3737
"github.com/moby/sys/mountinfo"
3838
"github.com/ugorji/go/codec"
39-
"github.com/wtsi-hgi/ibackup/internal"
39+
"github.com/wtsi-hgi/ibackup/errs"
4040
bolt "go.etcd.io/bbolt"
4141
)
4242

@@ -133,7 +133,7 @@ func (d *DB) GetFilesFromInode(inode uint64, mountPoint string) ([]string, error
133133

134134
v := b.Get(key)
135135
if v == nil {
136-
return internal.PathError{Msg: "key not found in inode bucket", Path: string(key)}
136+
return errs.PathError{Msg: "key not found in inode bucket", Path: string(key)}
137137
}
138138

139139
_, files = d.decodeIMPValue(v, de.Inode)
@@ -168,7 +168,7 @@ func (d *DB) RemoveFileFromInode(path string, inode uint64) error {
168168

169169
v := b.Get(key)
170170
if v == nil {
171-
return internal.PathError{Msg: "key not found in inode bucket", Path: string(key)}
171+
return errs.PathError{Msg: "key not found in inode bucket", Path: string(key)}
172172
}
173173

174174
_, files := d.decodeIMPValue(v, de.Inode)
@@ -242,7 +242,7 @@ func removePathFromInodeFiles(path string, files []string) ([]string, error) {
242242
func RemoveElementFromSlice(slice []string, element string) ([]string, error) {
243243
index := slices.Index(slice, element)
244244
if index < 0 {
245-
return nil, internal.PathError{Msg: ErrElementNotInSlice, Path: element}
245+
return nil, errs.PathError{Msg: ErrElementNotInSlice, Path: element}
246246
}
247247

248248
return slices.Delete(slice, index, index+1), nil

0 commit comments

Comments
 (0)