Skip to content

Commit f2848fe

Browse files
committed
errors,locking,ssh: use standard error joining
As of Go 1.24, if the Go version number in the "go.mod" file is set to 1.24 or higher, the "go vet" command now reports misuses of non-constant strings as format strings. In previous commits in this PR we have now resolved all but one of the instances where we provided a non-constant string as a format string. The remaining instance is our use of the Errorf() function of the "fmt" package from the Go standard library at the end of the Combine() function in our "errors" package. The Combine() function was added to our custom "errors" package in commit 08e3e5b of PR git-lfs#1870, originally for use in the "locking" package. This function merges multiple errors into a single error by concatenating their error messages with a newline separator character between each original message. In Go 1.20 the Join() function was added to the "errors" package of the Go standard library, and it performs the same concatenation of error messages as our Combine() function, including the use of a newline character as a separator between the original messages, except that it delays the concatenation until the Error() method is called. To resolve the remaining case where we pass a non-constant string as a format string, we remove the Combine() function from our "errors" package and replace it with a Join() function that simply invokes the Join() function of the standard library's "errors" package, which we can expect to be defined as we currently require the use of at least Go 1.21 (per the Go version specified in our "go.mod" file). To make this change, we alias the standard library's "errors" package as "goerrors", following the pattern established in our "lfshttp" package where we use that alias as well. We then revise the two callers of our Combine() function to make use of the new Join() function instead. One of these callers is the startConnection() function in our "ssh" package, which simply joins two errors, and the other caller is the FixLockableFileWriteFlags() method of the Client structure in our "locking" package, which collects zero or more errors while iterating over a list of files and repeatedly calling another method. Because the Join() functions accept variadic arguments rather than an array of errors, we revise these callers to pass the errors they intend to concatenate as direct arguments rather than in an array. For the FixLockableFileWriteFlags() method this means that as it iterates over its list of files, if any errors occur we immediately call the Join() function to add them to the error value we return at the end of the function, whereas previously we appended the errors to an array, and then called the Combine() function after the loop exited to concatenate any errors in the array into the function's final error return value.
1 parent ac44301 commit f2848fe

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

errors/errors.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ package errors
5050
// docs for more info: https://godoc.org/github.com/pkg/errors
5151

5252
import (
53-
"bytes"
53+
goerrors "errors"
5454
"fmt"
5555

5656
"github.com/pkg/errors"
@@ -102,19 +102,8 @@ func StackTrace(err error) []string {
102102
return nil
103103
}
104104

105-
func Combine(errs []error) error {
106-
if len(errs) == 0 {
107-
return nil
108-
}
109-
110-
var buf bytes.Buffer
111-
for i, err := range errs {
112-
if i > 0 {
113-
buf.WriteString("\n")
114-
}
115-
buf.WriteString(err.Error())
116-
}
117-
return fmt.Errorf(buf.String())
105+
func Join(errs ...error) error {
106+
return goerrors.Join(errs...)
118107
}
119108

120109
func Cause(err error) error {

locking/lockable.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ func (c *Client) FixLockableFileWriteFlags(files []string) error {
142142
return nil
143143
}
144144

145-
var errs []error
145+
var multiErr error
146146
for _, f := range files {
147147
err := c.fixSingleFileWriteFlags(f, c.getLockableFilter(), nil)
148148
if err != nil {
149-
errs = append(errs, err)
149+
multiErr = errors.Join(multiErr, err)
150150
}
151151
}
152152

153-
return errors.Combine(errs)
153+
return multiErr
154154
}
155155

156156
// fixSingleFileWriteFlags fixes write flags on a single file

ssh/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func startConnection(id int, osEnv config.Environment, gitEnv config.Environment
7979
r.Close()
8080
w.Close()
8181
cmd.Wait()
82-
err = errors.Combine([]error{err, errors.New(tr.Tr.Get("Failed to connect to remote SSH server: %s", cmd.Stderr))})
82+
err = errors.Join(err, errors.New(tr.Tr.Get("Failed to connect to remote SSH server: %s", cmd.Stderr)))
8383
tracerx.Printf("pure SSH connection unsuccessful (#%d)", id)
8484
} else {
8585
tracerx.Printf("pure SSH connection successful (#%d)", id)

0 commit comments

Comments
 (0)