mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-03 07:12:27 +00:00
lib: Wrap errors with errors.Wrap instead of fmt.Errorf (#6181)
This commit is contained in:
parent
e2f6d0d6c4
commit
cf312abc72
@ -10,7 +10,6 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@ -22,6 +21,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/fs"
|
||||
"github.com/syncthing/syncthing/lib/protocol"
|
||||
"github.com/syncthing/syncthing/lib/rand"
|
||||
@ -120,13 +121,13 @@ func NewWithFreePorts(myID protocol.DeviceID) (Configuration, error) {
|
||||
|
||||
port, err := getFreePort("127.0.0.1", DefaultGUIPort)
|
||||
if err != nil {
|
||||
return Configuration{}, fmt.Errorf("get free port (GUI): %v", err)
|
||||
return Configuration{}, errors.Wrap(err, "get free port (GUI)")
|
||||
}
|
||||
cfg.GUI.RawAddress = fmt.Sprintf("127.0.0.1:%d", port)
|
||||
|
||||
port, err = getFreePort("0.0.0.0", DefaultTCPPort)
|
||||
if err != nil {
|
||||
return Configuration{}, fmt.Errorf("get free port (BEP): %v", err)
|
||||
return Configuration{}, errors.Wrap(err, "get free port (BEP)")
|
||||
}
|
||||
if port == DefaultTCPPort {
|
||||
cfg.Options.ListenAddresses = []string{"default"}
|
||||
|
@ -11,12 +11,12 @@ package connections
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/config"
|
||||
"github.com/syncthing/syncthing/lib/connections/registry"
|
||||
@ -75,7 +75,7 @@ func (d *quicDialer) Dial(_ protocol.DeviceID, uri *url.URL) (internalConn, erro
|
||||
if createdConn != nil {
|
||||
_ = createdConn.Close()
|
||||
}
|
||||
return internalConn{}, fmt.Errorf("dial: %v", err)
|
||||
return internalConn{}, errors.Wrap(err, "dial")
|
||||
}
|
||||
|
||||
stream, err := session.OpenStreamSync(ctx)
|
||||
@ -85,7 +85,7 @@ func (d *quicDialer) Dial(_ protocol.DeviceID, uri *url.URL) (internalConn, erro
|
||||
if createdConn != nil {
|
||||
_ = createdConn.Close()
|
||||
}
|
||||
return internalConn{}, fmt.Errorf("open stream: %v", err)
|
||||
return internalConn{}, errors.Wrap(err, "open stream")
|
||||
}
|
||||
|
||||
return internalConn{&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, quicPriority}, nil
|
||||
|
@ -18,6 +18,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/fs"
|
||||
"github.com/syncthing/syncthing/lib/osutil"
|
||||
"github.com/syncthing/syncthing/lib/sync"
|
||||
@ -404,14 +406,14 @@ func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd Chan
|
||||
// Pattern is rooted in the current dir only
|
||||
pattern.match, err = glob.Compile(line[1:], '/')
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
||||
return errors.Wrapf(err, "invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, pattern)
|
||||
} else if strings.HasPrefix(line, "**/") {
|
||||
// Add the pattern as is, and without **/ so it matches in current dir
|
||||
pattern.match, err = glob.Compile(line, '/')
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
||||
return errors.Wrapf(err, "invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, pattern)
|
||||
|
||||
@ -419,7 +421,7 @@ func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd Chan
|
||||
pattern.pattern = line
|
||||
pattern.match, err = glob.Compile(line, '/')
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
||||
return errors.Wrapf(err, "invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, pattern)
|
||||
} else {
|
||||
@ -427,7 +429,7 @@ func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd Chan
|
||||
// current directory and subdirs.
|
||||
pattern.match, err = glob.Compile(line, '/')
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
||||
return errors.Wrapf(err, "invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, pattern)
|
||||
|
||||
@ -435,7 +437,7 @@ func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd Chan
|
||||
pattern.pattern = line
|
||||
pattern.match, err = glob.Compile(line, '/')
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
||||
return errors.Wrapf(err, "invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, pattern)
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
@ -21,6 +20,9 @@ import (
|
||||
stdsync "sync"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/thejerf/suture"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/config"
|
||||
"github.com/syncthing/syncthing/lib/connections"
|
||||
"github.com/syncthing/syncthing/lib/db"
|
||||
@ -35,7 +37,6 @@ import (
|
||||
"github.com/syncthing/syncthing/lib/upgrade"
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
"github.com/syncthing/syncthing/lib/versioner"
|
||||
"github.com/thejerf/suture"
|
||||
)
|
||||
|
||||
// How many files to send in each Index/IndexUpdate message.
|
||||
@ -1799,7 +1800,7 @@ func (m *model) SetIgnores(folder string, content []string) error {
|
||||
err := cfg.CheckPath()
|
||||
if err == config.ErrPathMissing {
|
||||
if err = cfg.CreateRoot(); err != nil {
|
||||
return fmt.Errorf("failed to create folder root: %v", err)
|
||||
return errors.Wrap(err, "failed to create folder root")
|
||||
}
|
||||
err = cfg.CheckPath()
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"path"
|
||||
@ -15,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
lz4 "github.com/bkaradzic/go-lz4"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -397,7 +397,7 @@ func (c *rawConnection) dispatcherLoop() (err error) {
|
||||
return fmt.Errorf("protocol error: index message in state %d", state)
|
||||
}
|
||||
if err := checkIndexConsistency(msg.Files); err != nil {
|
||||
return fmt.Errorf("protocol error: index: %v", err)
|
||||
return errors.Wrap(err, "protocol error: index")
|
||||
}
|
||||
c.handleIndex(*msg)
|
||||
state = stateReady
|
||||
@ -408,7 +408,7 @@ func (c *rawConnection) dispatcherLoop() (err error) {
|
||||
return fmt.Errorf("protocol error: index update message in state %d", state)
|
||||
}
|
||||
if err := checkIndexConsistency(msg.Files); err != nil {
|
||||
return fmt.Errorf("protocol error: index update: %v", err)
|
||||
return errors.Wrap(err, "protocol error: index update")
|
||||
}
|
||||
c.handleIndexUpdate(*msg)
|
||||
state = stateReady
|
||||
@ -419,7 +419,7 @@ func (c *rawConnection) dispatcherLoop() (err error) {
|
||||
return fmt.Errorf("protocol error: request message in state %d", state)
|
||||
}
|
||||
if err := checkFilename(msg.Name); err != nil {
|
||||
return fmt.Errorf("protocol error: request: %q: %v", msg.Name, err)
|
||||
return errors.Wrapf(err, "protocol error: request: %q", msg.Name)
|
||||
}
|
||||
go c.handleRequest(*msg)
|
||||
|
||||
@ -468,7 +468,7 @@ func (c *rawConnection) readMessageAfterHeader(hdr Header, fourByteBuf []byte) (
|
||||
// First comes a 4 byte message length
|
||||
|
||||
if _, err := io.ReadFull(c.cr, fourByteBuf[:4]); err != nil {
|
||||
return nil, fmt.Errorf("reading message length: %v", err)
|
||||
return nil, errors.Wrap(err, "reading message length")
|
||||
}
|
||||
msgLen := int32(binary.BigEndian.Uint32(fourByteBuf))
|
||||
if msgLen < 0 {
|
||||
@ -481,7 +481,7 @@ func (c *rawConnection) readMessageAfterHeader(hdr Header, fourByteBuf []byte) (
|
||||
|
||||
buf := BufferPool.Get(int(msgLen))
|
||||
if _, err := io.ReadFull(c.cr, buf); err != nil {
|
||||
return nil, fmt.Errorf("reading message: %v", err)
|
||||
return nil, errors.Wrap(err, "reading message")
|
||||
}
|
||||
|
||||
// ... which might be compressed
|
||||
@ -494,7 +494,7 @@ func (c *rawConnection) readMessageAfterHeader(hdr Header, fourByteBuf []byte) (
|
||||
decomp, err := c.lz4Decompress(buf)
|
||||
BufferPool.Put(buf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decompressing message: %v", err)
|
||||
return nil, errors.Wrap(err, "decompressing message")
|
||||
}
|
||||
buf = decomp
|
||||
|
||||
@ -509,7 +509,7 @@ func (c *rawConnection) readMessageAfterHeader(hdr Header, fourByteBuf []byte) (
|
||||
return nil, err
|
||||
}
|
||||
if err := msg.Unmarshal(buf); err != nil {
|
||||
return nil, fmt.Errorf("unmarshalling message: %v", err)
|
||||
return nil, errors.Wrap(err, "unmarshalling message")
|
||||
}
|
||||
BufferPool.Put(buf)
|
||||
|
||||
@ -520,7 +520,7 @@ func (c *rawConnection) readHeader(fourByteBuf []byte) (Header, error) {
|
||||
// First comes a 2 byte header length
|
||||
|
||||
if _, err := io.ReadFull(c.cr, fourByteBuf[:2]); err != nil {
|
||||
return Header{}, fmt.Errorf("reading length: %v", err)
|
||||
return Header{}, errors.Wrap(err, "reading length")
|
||||
}
|
||||
hdrLen := int16(binary.BigEndian.Uint16(fourByteBuf))
|
||||
if hdrLen < 0 {
|
||||
@ -531,12 +531,12 @@ func (c *rawConnection) readHeader(fourByteBuf []byte) (Header, error) {
|
||||
|
||||
buf := BufferPool.Get(int(hdrLen))
|
||||
if _, err := io.ReadFull(c.cr, buf); err != nil {
|
||||
return Header{}, fmt.Errorf("reading header: %v", err)
|
||||
return Header{}, errors.Wrap(err, "reading header")
|
||||
}
|
||||
|
||||
var hdr Header
|
||||
if err := hdr.Unmarshal(buf); err != nil {
|
||||
return Header{}, fmt.Errorf("unmarshalling header: %v", err)
|
||||
return Header{}, errors.Wrap(err, "unmarshalling header")
|
||||
}
|
||||
|
||||
BufferPool.Put(buf)
|
||||
@ -558,7 +558,7 @@ func (c *rawConnection) handleIndexUpdate(im IndexUpdate) {
|
||||
func checkIndexConsistency(fs []FileInfo) error {
|
||||
for _, f := range fs {
|
||||
if err := checkFileInfoConsistency(f); err != nil {
|
||||
return fmt.Errorf("%q: %v", f.Name, err)
|
||||
return errors.Wrapf(err, "%q", f.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@ -707,12 +707,12 @@ func (c *rawConnection) writeCompressedMessage(msg message) error {
|
||||
size := msg.ProtoSize()
|
||||
buf := BufferPool.Get(size)
|
||||
if _, err := msg.MarshalTo(buf); err != nil {
|
||||
return fmt.Errorf("marshalling message: %v", err)
|
||||
return errors.Wrap(err, "marshalling message")
|
||||
}
|
||||
|
||||
compressed, err := c.lz4Compress(buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("compressing message: %v", err)
|
||||
return errors.Wrap(err, "compressing message")
|
||||
}
|
||||
|
||||
hdr := Header{
|
||||
@ -731,7 +731,7 @@ func (c *rawConnection) writeCompressedMessage(msg message) error {
|
||||
binary.BigEndian.PutUint16(buf, uint16(hdrSize))
|
||||
// Header
|
||||
if _, err := hdr.MarshalTo(buf[2:]); err != nil {
|
||||
return fmt.Errorf("marshalling header: %v", err)
|
||||
return errors.Wrap(err, "marshalling header")
|
||||
}
|
||||
// Message length
|
||||
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(len(compressed)))
|
||||
@ -744,7 +744,7 @@ func (c *rawConnection) writeCompressedMessage(msg message) error {
|
||||
|
||||
l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message (%d uncompressed)), err=%v", n, hdrSize, len(compressed), size, err)
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing message: %v", err)
|
||||
return errors.Wrap(err, "writing message")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -767,13 +767,13 @@ func (c *rawConnection) writeUncompressedMessage(msg message) error {
|
||||
binary.BigEndian.PutUint16(buf, uint16(hdrSize))
|
||||
// Header
|
||||
if _, err := hdr.MarshalTo(buf[2:]); err != nil {
|
||||
return fmt.Errorf("marshalling header: %v", err)
|
||||
return errors.Wrap(err, "marshalling header")
|
||||
}
|
||||
// Message length
|
||||
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(size))
|
||||
// Message
|
||||
if _, err := msg.MarshalTo(buf[2+hdrSize+4:]); err != nil {
|
||||
return fmt.Errorf("marshalling message: %v", err)
|
||||
return errors.Wrap(err, "marshalling message")
|
||||
}
|
||||
|
||||
n, err := c.cw.Write(buf[:totSize])
|
||||
@ -781,7 +781,7 @@ func (c *rawConnection) writeUncompressedMessage(msg message) error {
|
||||
|
||||
l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message), err=%v", n, hdrSize, size, err)
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing message: %v", err)
|
||||
return errors.Wrap(err, "writing message")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/dialer"
|
||||
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
|
||||
"github.com/syncthing/syncthing/lib/relay/protocol"
|
||||
@ -225,7 +227,7 @@ func performHandshakeAndValidation(conn *tls.Conn, uri *url.URL) error {
|
||||
if relayIDs != "" {
|
||||
relayID, err := syncthingprotocol.DeviceIDFromString(relayIDs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("relay address contains invalid verification id: %s", err)
|
||||
return errors.Wrap(err, "relay address contains invalid verification id")
|
||||
}
|
||||
|
||||
certs := cs.PeerCertificates
|
||||
|
@ -20,6 +20,8 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/rand"
|
||||
)
|
||||
|
||||
@ -92,7 +94,7 @@ func SecureDefault() *tls.Config {
|
||||
func NewCertificate(certFile, keyFile, commonName string, lifetimeDays int) (tls.Certificate, error) {
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("generate key: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "generate key")
|
||||
}
|
||||
|
||||
notBefore := time.Now().Truncate(24 * time.Hour)
|
||||
@ -115,39 +117,39 @@ func NewCertificate(certFile, keyFile, commonName string, lifetimeDays int) (tls
|
||||
|
||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("create cert: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "create cert")
|
||||
}
|
||||
|
||||
certOut, err := os.Create(certFile)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save cert: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
||||
}
|
||||
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save cert: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
||||
}
|
||||
err = certOut.Close()
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save cert: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
||||
}
|
||||
|
||||
keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
}
|
||||
|
||||
block, err := pemBlockForKey(priv)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
}
|
||||
|
||||
err = pem.Encode(keyOut, block)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
}
|
||||
err = keyOut.Close()
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %s", err)
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
}
|
||||
|
||||
return tls.LoadX509KeyPair(certFile, keyFile)
|
||||
|
Loading…
Reference in New Issue
Block a user