Skip to content

Commit a2b1fef

Browse files
committed
commands,creds,lfs,tools: use error join function
In a previous commit in this PR we replaced the Combine() function in our custom "errors" package with a Join() function that passes its arguments through to the Join() function of the Go standard library's "errors" package. Both these Join() functions and our earlier Combine() function serve to concatenate multiple error values into a single error value, with the individual error messages separated by newline characters when the error is converted to a string value for output. In a number of instances we perform the same concatenation of error messages using loops and assembling the final error message by explicitly appending each individual error message along with the newline separator characters. We can now simplify all these instances where we need to concatenate error messages by making use of the Join() function in our "errors" package. In almost all of these cases, this means we are able to eliminate the conditional logic used to check if a newline separator character is required because more than one error has been encountered. (The exception is in the Fill() method of the CredentialHelpers structure of our "creds" package where we previously concatenated the error messages using the Join() method of the "strings" package from the Go standard library.) Note that we could also have used the Combine() function from our "errors" package to simplify these use cases; however, the Join() function is more convenient in most instances because it accepts variadic arguments rather than requiring an array of errors as input.
1 parent f2848fe commit a2b1fef

File tree

6 files changed

+20
-63
lines changed

6 files changed

+20
-63
lines changed

commands/command_fetch.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package commands
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"os"
76
"sync"
87
"time"
98

9+
"github.com/git-lfs/git-lfs/v3/errors"
1010
"github.com/git-lfs/git-lfs/v3/filepathfilter"
1111
"github.com/git-lfs/git-lfs/v3/git"
1212
"github.com/git-lfs/git-lfs/v3/lfs"
@@ -184,11 +184,7 @@ func pointersToFetchForRef(ref string, filter *filepathfilter.Filter) ([]*lfs.Wr
184184
var multiErr error
185185
tempgitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
186186
if err != nil {
187-
if multiErr != nil {
188-
multiErr = fmt.Errorf("%v\n%v", multiErr, err)
189-
} else {
190-
multiErr = err
191-
}
187+
multiErr = errors.Join(multiErr, err)
192188
return
193189
}
194190

@@ -227,11 +223,7 @@ func pointersToFetchForRefs(refs []string) ([]*lfs.WrappedPointer, error) {
227223
var numObjs int64
228224
tempgitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
229225
if err != nil {
230-
if multiErr != nil {
231-
multiErr = fmt.Errorf("%v\n%v", multiErr, err)
232-
} else {
233-
multiErr = err
234-
}
226+
multiErr = errors.Join(multiErr, err)
235227
return
236228
}
237229

@@ -362,11 +354,7 @@ func scanAll() []*lfs.WrappedPointer {
362354
var numObjs int64
363355
tempgitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
364356
if err != nil {
365-
if multiErr != nil {
366-
multiErr = fmt.Errorf("%v\n%v", multiErr, err)
367-
} else {
368-
multiErr = err
369-
}
357+
multiErr = errors.Join(multiErr, err)
370358
return
371359
}
372360

commands/uploader.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"fmt"
54
"io"
65
"net/url"
76
"os"
@@ -139,11 +138,7 @@ func (c *uploadContext) addScannerError(err error) {
139138
c.errMu.Lock()
140139
defer c.errMu.Unlock()
141140

142-
if c.scannerErr != nil {
143-
c.scannerErr = fmt.Errorf("%v\n%v", c.scannerErr, err)
144-
} else {
145-
c.scannerErr = err
146-
}
141+
c.scannerErr = errors.Join(c.scannerErr, err)
147142
}
148143

149144
func (c *uploadContext) buildGitScanner() *lfs.GitScanner {

creds/creds.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ var credHelperNoOp = errors.New("no-op!")
500500
// helpers are added to the skip list, and never attempted again for the
501501
// lifetime of the current Git LFS command.
502502
func (s *CredentialHelpers) Fill(what Creds) (Creds, error) {
503-
errs := make([]string, 0, len(s.helpers))
503+
var multiErr error
504504
for i, h := range s.helpers {
505505
if s.skipped(i) {
506506
continue
@@ -511,7 +511,7 @@ func (s *CredentialHelpers) Fill(what Creds) (Creds, error) {
511511
if err != credHelperNoOp {
512512
s.skip(i)
513513
tracerx.Printf("credential fill error: %s", err)
514-
errs = append(errs, err.Error())
514+
multiErr = errors.Join(multiErr, err)
515515
}
516516
continue
517517
}
@@ -521,11 +521,11 @@ func (s *CredentialHelpers) Fill(what Creds) (Creds, error) {
521521
}
522522
}
523523

524-
if len(errs) > 0 {
525-
return nil, errors.New(tr.Tr.Get("credential fill errors:\n%s", strings.Join(errs, "\n")))
524+
if multiErr != nil {
525+
multiErr = errors.Join(errors.New(tr.Tr.Get("credential fill errors:")), multiErr)
526526
}
527527

528-
return nil, nil
528+
return nil, multiErr
529529
}
530530

531531
// Reject implements CredentialHelper.Reject and rejects the given Creds "what"

lfs/gitfilter_smudge.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,7 @@ func (f *GitFilter) downloadFile(writer io.Writer, ptr *Pointer, workingfile, me
123123
q.Wait()
124124

125125
if errs := q.Errors(); len(errs) > 0 {
126-
var multiErr error
127-
for _, e := range errs {
128-
if multiErr != nil {
129-
multiErr = fmt.Errorf("%v\n%v", multiErr, e)
130-
} else {
131-
multiErr = e
132-
}
133-
}
134-
135-
return 0, errors.Wrap(multiErr, tr.Tr.Get("Error downloading %s (%s)", workingfile, ptr.Oid))
126+
return 0, errors.Wrap(errors.Join(errs...), tr.Tr.Get("Error downloading %s (%s)", workingfile, ptr.Oid))
136127
}
137128

138129
return f.readLocalFile(writer, ptr, mediafile, workingfile, nil)
@@ -155,15 +146,7 @@ func (f *GitFilter) downloadFileFallBack(writer io.Writer, ptr *Pointer, working
155146
q.Wait()
156147

157148
if errs := q.Errors(); len(errs) > 0 {
158-
var multiErr error
159-
for _, e := range errs {
160-
if multiErr != nil {
161-
multiErr = fmt.Errorf("%v\n%v", multiErr, e)
162-
} else {
163-
multiErr = e
164-
}
165-
}
166-
wrappedError := errors.Wrap(multiErr, tr.Tr.Get("Error downloading %s (%s)", workingfile, ptr.Oid))
149+
wrappedError := errors.Wrap(errors.Join(errs...), tr.Tr.Get("Error downloading %s (%s)", workingfile, ptr.Oid))
167150
if index >= len(remotes)-1 {
168151
return 0, wrappedError
169152
} else {

lfs/scanner_git_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package lfs_test // to avoid import cycles
55
// which avoids import cycles with testutils
66

77
import (
8-
"fmt"
98
"sort"
109
"testing"
1110
"time"
1211

1312
"github.com/git-lfs/git-lfs/v3/config"
13+
"github.com/git-lfs/git-lfs/v3/errors"
1414
. "github.com/git-lfs/git-lfs/v3/lfs"
1515
test "github.com/git-lfs/git-lfs/v3/t/cmd/util"
1616
"github.com/stretchr/testify/assert"
@@ -94,11 +94,7 @@ func scanUnpushed(remoteName string) ([]*WrappedPointer, error) {
9494

9595
gitscanner := NewGitScanner(config.New(), func(p *WrappedPointer, err error) {
9696
if err != nil {
97-
if multiErr != nil {
98-
multiErr = fmt.Errorf("%v\n%v", multiErr, err)
99-
} else {
100-
multiErr = err
101-
}
97+
multiErr = errors.Join(multiErr, err)
10298
return
10399
}
104100

tools/channels.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package tools
22

3-
import "fmt"
3+
import "github.com/git-lfs/git-lfs/v3/errors"
44

55
// Interface for all types of wrapper around a channel of results and an error channel
66
// Implementors will expose a type-specific channel for results
@@ -17,18 +17,13 @@ type BaseChannelWrapper struct {
1717
}
1818

1919
func (w *BaseChannelWrapper) Wait() error {
20-
var err error
21-
for e := range w.errorChan {
22-
if err != nil {
23-
// Combine in case multiple errors
24-
err = fmt.Errorf("%v\n%v", err, e)
25-
26-
} else {
27-
err = e
28-
}
20+
var multiErr error
21+
for err := range w.errorChan {
22+
// Combine in case multiple errors
23+
multiErr = errors.Join(multiErr, err)
2924
}
3025

31-
return err
26+
return multiErr
3227
}
3328

3429
func NewBaseChannelWrapper(errChan <-chan error) *BaseChannelWrapper {

0 commit comments

Comments
 (0)